5 users online | 5 Guests and 0 Registered

Can I submit DOS commands from SAS and see the results in the LOG?


One method for passing a DOS command to the host system is to use the PIPE option in a FILENAME.

A utility macro programme to issue a DOS Command might look like this:

%macro dos_cmd (cmd) ;
  %local f_ref rc rc2 cmd_scrape ;
  *** Generate a FILEREF using a DOS PIPE *** ;
  %let rc = %sysfunc (filename (f_ref, &cmd, pipe)) ;
  %if &rc = 0 %then 
  %do ;
    *** Open the FILEREF using Sequential Access *** ;
    %let rc2 = %sysfunc (fopen (&f_ref, s)) ;
	%if &rc2 ne 0 %then 
    %do ;
	  *** Read a record from the FILEREF into a File Data Buffer *** ;
	  %do %while(%sysfunc(fread(&rc2)) = 0) ;
	    *** Copy data from the File Data Buffer into a variable, then write the line into the LOG *** ;
	    %let rc3 = %qsysfunc(fget(&rc2,cmd_scrape,400)) ;
	    %put &cmd_scrape ;
	  %end ;
	  *** Close the FILEREF *** ;
	  %let rc2 = %sysfunc (fclose (&rc2)) ;
	%end ;
	%else 
        %do ;
	  %put ERROR: The Command could not be executed: %sysfunc(sysmsg()). ;
 	%end ;
	*** De-assign the FILEREF *** ;
	%let rc = %sysfunc (filename (fileref)) ;
  %end ;
  %else 
  %do ;
    %put ERROR: The Command Box could not be accessed. ;
  %end ;
%mend dos_cmd;

Passing the DOS Command as a macro parameter may require Macro Quoting to mask special characters:

%dos_cmd(%nrstr(cd c:\ && dir /og))

This is a double, conditional command: firstly cd c:\ causes the directory to change from the default to the C: Drive, a single & would then execute the next DOS Command, the double && will only execute the next command on successful completion of the first.  The second command dir /og lists the direcory contents in alphabetical (O)rder (G)rouped by type.

Author:
Alan D Rudland
Revision:
1.0
Average rating:0 (0 Votes)

You cannot comment on this entry

Chuck Norris has counted to infinity. Twice.

Records in this category

Tags