Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check available disk space using SAS

Tags:

sas

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

like image 904
Tazz Avatar asked Mar 07 '23 12:03

Tazz


2 Answers

I've basically modified the code available in this link according to your requirement. I've also added a bit of code to fix issues faced due to quotes and the pipe command. Basically you should let SAS deal with quotes before passing on the 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 how to use this macro for the C: drive

%windows_bytes_free(c:);
like image 151
Raunak Thomas Avatar answered Mar 19 '23 02:03

Raunak Thomas


Tazz:

Presuming you are running SAS on a Windows platform -- Piping wmic command output into SAS can deliver vast amounts of information about the system, including the freespace on the disks.

WMIC - Using Windows Management Instrumentation Command-line; 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 can also export it's information in XML format -- the output is more complicated but extremely capable. This sample code uses SAS' xmlv2 engine and the automap= option:

* 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;
like image 22
Richard Avatar answered Mar 19 '23 02:03

Richard