Tuesday, January 19, 2010

PIPE mode in external file functions

To get the output of shell command within SAS, we have to resort to PIPE.
Generally the job is using DATA step input/output.
We can also use external file funcitons to handle it, although it is more complex.


%let command = hostname;

data _null_;
length value $ 20;
rc = filename('myfile', "&command", 'PIPE');
fid = fopen('myfile', 'S');
rc = fread(fid);
rc = fget(fid, value, 20);
rc = fclose(fid);
rc = filename('myfile');

put "Command output:" value;
run;

%let filerf=myfile;
%let rc = %sysfunc(filename(filerf, &command, pipe));
%let fid = %sysfunc(fopen(&filerf, S));
%let rc = %sysfunc(fread(&fid));
%let rc = %sysfunc(fget(&fid, value, 20));
%let rc = %sysfunc(fclose(&fid));
%let rc = %sysfunc(filename(filerf));

%put Command output: &value;