Getting the outcome of a system command into SAS macro variable?

  • Follow


I know that in Unix I can use FILENAME PIPE command then use DATA STEP to 
read in results of a system command.  For example,

filename abc PIPE "ls -al";
data work.xyz;
   infile abc;
   input ...;
   call symput...;
;

However, is there an elegant way to assign an one-line outcome of an Unix 
command into a SAS macro variable?  I can use %sysexec to execute the Unix 
command, direct it into a text file, then read it in using DATA STEP but 
isn't there a direct way to pass the output to SAS? 

0
Reply kenneth_m_lin (421) 3/27/2010 6:10:16 AM

On Mar 26, 11:10=A0pm, "Kenneth M. Lin" <kenneth_m_...@sbcglobal.net>
wrote:
> I know that in Unix I can use FILENAME PIPE command then use DATA STEP to
> read in results of a system command. =A0For example,
>
> filename abc PIPE "ls -al";
> data work.xyz;
> =A0 =A0infile abc;
> =A0 =A0input ...;
> =A0 =A0call symput...;
> ;
>
> However, is there an elegant way to assign an one-line outcome of an Unix
> command into a SAS macro variable? =A0I can use %sysexec to execute the U=
nix
> command, direct it into a text file, then read it in using DATA STEP but
> isn't there a direct way to pass the output to SAS?

Hi,

  You can set up a pipe in the macro language, not very elegant.

  I don't have SAS right now so this is from memory. There are
probably many syntax errors.

UNIX example

%let rc=3D%sysfunc(filename(fname,echo getthis ,pipe));
%let fop=3D%sysfunc(fopen(&fname,,,i));
%let rc=3D%sysfunc(fget(&fop,txt));
%let rc=3D%sysfunc(fclose(&fop));
%let rc=3D%sysfunc(fclose(&fop));

Macro variable Txt should have getthis

I have several examples of reading files and SAS datasets on my site

http://homepage.mac.com/magdelina/.Public/utl.html
utl_tipweb
see
/* T002510 READING A SAS DATASET AND A FLATILE USIN THE MACRO LANGUAGE
Here is an excerpt

/* create a sample file fo input to macro read code */
 filename tmp temp;
   data _null_;
     set sashelp.class;
     file tmp;
     put @1 name @10 age @20 age;
   run;

/* read the file in the macro language */
 %macro readfile(tmp);
     %let fid=3D%sysfunc(fopen(tmp,s));
     %do %while(%sysfunc(fread(&fid)) EQ 0);
         %let rc=3D%sysfunc(fget(&fid,str,200));
         %put &str;
     %end;
     %let rc=3D%sysfunc(fclose(&fid));
   %mend readfile;
   %readfile;




0
Reply xlr82sas 3/27/2010 7:23:29 AM


On Mar 27, 1:10=A0am, "Kenneth M. Lin" <kenneth_m_...@sbcglobal.net>
wrote:
> I know that in Unix I can use FILENAME PIPE command then use DATA STEP to
> read in results of a system command. =A0For example,
>
> filename abc PIPE "ls -al";
> data work.xyz;
> =A0 =A0infile abc;
> =A0 =A0input ...;
> =A0 =A0call symput...;
> ;
>
> However, is there an elegant way to assign an one-line outcome of an Unix
> command into a SAS macro variable? =A0I can use %sysexec to execute the U=
nix
> command, direct it into a text file, then read it in using DATA STEP but
> isn't there a direct way to pass the output to SAS?

Kenneth,

If you mean you want to do some task once for each outcome of the Unix
command....



    filename abc PIPE "ls -al";

 data work.xyz;
    infile abc;
    input ...;
  run;

proc sql noprint;
  select count(*) into :noofresults
  from xyz;
  ;
quit;

%macro mymacro;

 %do k =3D 1 %to &noofresults;

 data _null_;
   set xyz;
   if _n_ =3D &k;
    call symput("variable&k.",input);
  run;

  data &&variable&k;
     set ...;
  run;

%end;

  %mend;

%mymacro;




0
Reply MichelleZ 3/27/2010 12:28:29 PM

On Mar 27, 8:10=A0am, "Kenneth M. Lin" <kenneth_m_...@sbcglobal.net>
wrote:
> I know that in Unix I can use FILENAME PIPE command then use DATA STEP to
> read in results of a system command. =A0For example,
>
> filename abc PIPE "ls -al";
> data work.xyz;
> =A0 =A0infile abc;
> =A0 =A0input ...;
> =A0 =A0call symput...;
> ;
>
> However, is there an elegant way to assign an one-line outcome of an Unix
> command into a SAS macro variable? =A0I can use %sysexec to execute the U=
nix
> command, direct it into a text file, then read it in using DATA STEP but
> isn't there a direct way to pass the output to SAS?

%let dsid=3D%sysfunc(open(&ds,is));
%if &dsid EQ 0 %then %do;
  %put ERROR: (varnum) Dataset &ds not opened due to the following
reason:;
  %put %sysfunc(sysmsg());
%end;
0
Reply RolandRB 3/29/2010 9:53:30 AM

3 Replies
530 Views

(page loaded in 0.097 seconds)

Similiar Articles:













7/21/2012 5:46:04 PM


Reply: