Showing posts with label %sysfunc. Show all posts
Showing posts with label %sysfunc. Show all posts

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;

Wednesday, January 21, 2009

a special function for %sysfunc world: filename function

Below are the code from SAS online doc:
%let filrf=myfile;
%let rc=%sysfunc(filename(filrf, physical-filename));
%if &rc ne 0 %then
%put %sysfunc(sysmsg());
%let rc=%sysfunc(filename(filrf));

It should be noted that the fileref is "myfile", not "filerf". That means the macro variable "filerf" contains the ACTUAL fileref.
When the macro variable does not exist, the system generates a fileref automatically.

It seems that it is unique to filename function.
For now, I have not found any other functions which can take the similar behavior.

Note that it will not work if the fileref has been assigned. For more information, please see http://support.sas.com/kb/6/567.html