How to check available disk space using SAS

How can I check the space left on the disk and if it is less than 1 GB to display a message using SAS. I only have code that checks the size of the SAS file.

+4
source share
2 answers

I basically changed the code available in this link to suit your requirement. I also added some code to troubleshoot quotes and pipe issues. Basically, you should let SAS handle quotes before passing code.

%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;

An example of using this macro for drive C:

%windows_bytes_free(c:);
+5
source

Tazz:

, SAS Windows. Piping wmic SAS , .

WMIC - Windows; 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 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;
+2

Source: https://habr.com/ru/post/1689736/


All Articles