如何使用SAS检查可用磁盘空间

时间:2017-11-23 12:13:01

标签: sas

如何检查驱动器上剩余的空间以及是否小于1GB以使用SAS输出消息。 我只有一个检查SAS文件大小的代码。

2 个答案:

答案 0 :(得分:5)

我基本上已根据您的要求修改了this链接中可用的代码。我还添加了一些代码来修复由于引号和管道命令而面临的问题。基本上你应该让SAS在传递代码之前处理引号。

%macro windows_bytes_free(sm_path);

%global mv_bytes_free;
%let mv_bytes_free = -1;  /* In case of error */
%let filepath = %sysfunc(quote(%qsysfunc(dequote(&sm_path)))); /* To prevent issues with quotes remove quotes if present and apply it again*/

/* Run the DIR command and retrieve results using an unnamed pipe */
filename tempdir pipe %sysfunc(quote(dir /-c  &filepath  | find "bytes free")) ;

    data _null_;
        infile tempdir length=reclen ;
        input line $varying1024. reclen ;

        re = prxparse('/([0-9]+) bytes/');  /* Parse the output of DIR using a Perl regular expression */

        if prxmatch(re, line) then do;
            bytes_str = prxposn(re, 1, line);
            bytes = input(bytes_str, 20.);
            call symput('mv_bytes_free', bytes);    /* Assign available disk space in bytes to a global macro variable */
            kb = bytes /1024;
            mb = kb / 1024;
            gb = mb / 1024;
            format bytes comma20.0;
            format kb mb gb comma20.1;

            /* Write a note to the SAS log */
            put "NOTE: &sm_path " bytes= kb= mb= gb=;
            if gb<1 then put '** Available space is less than 1 gb';
            else put '** Enough space is available';

        end;

    run;

    %if &mv_bytes_free eq -1 %then %put ERROR: error in windows_bytes_free macro;

%mend;

如何将此宏用于C:驱动器

的示例
%windows_bytes_free(c:);

答案 1 :(得分:2)

TAZZ:

假设您在Windows平台上运行SAS - 将管道wmic命令输出到SAS可以提供​​有关系统的大量信息,包括磁盘上的可用空间。

WMIC - 使用Windows Management Instrumentation命令行; https://msdn.microsoft.com/en-us/library/aa394531(v=vs.85).aspx;

%let csvdata = %sysfunc(pathname(work))\wmic_output.csv;

filename wmic_csv "&csvdata" encoding="utf-16";
filename gather pipe "wmic logicaldisk get name,size,freespace /format:csv"; 

* process the wmic command and strip off blank first row and extraneous CR character at end of line;
data _null_;
  infile gather; 
  input;
  if _n_ > 1;
  _infile_ = compress(_infile_, '0d'x);
  file wmic_csv;
  put _infile_;
run;

proc import replace out=diskinfo file=wmic_csv dbms=csv;
run;

data _null_;
  set diskinfo;
  if freespace < 1e9 then put "WARNING: " name "has remaining" freespace=;
run;

wmic也可以以XML格式导出它的信息 - 输出更复杂但功能更强大。此示例代码使用SAS&#39; xmlv2引擎和automap=选项:

* WMIC - Using Windows Management Instrumentation Command-line;
* https://msdn.microsoft.com/en-us/library/aa394531(v=vs.85).aspx;

%let xmldata = %sysfunc(pathname(work))\wmic_output.xml;
%let xmlautomap  = %sysfunc(pathname(work))\wmic_output-automap.xml;
%let xmlmap =  %sysfunc(pathname(work))\wmic_output-map.xml;

filename wmic "&xmldata" encoding="utf-16";
filename wmicmap "&xmlmap";

filename gather pipe "wmic logicaldisk get name,size,freespace /format:rawxml > ""&xmldata""";

data _null_;
  infile gather;
  input;
  put _infile_;
  rc = sleep(.1,1);
run;

libname wmic xmlv2 automap=replace xmlmap=wmicmap;

proc copy in=wmic out=work;
run;

proc transpose data=work.property out=properties(drop=_name_) suffix=_text;
  by instance_ordinal;
  id property_name;
  var value; 
run;

filename gather;
filename wmic;
filename wmicmap;