Download stdout.sas stdout.sasSubmit a comment

/* stdout
 * Richard A. DeVenezia
 * June 10, 2004  RIPRR = RIPGIPPER
 * http://www.devenezia.com
 *
 * Place the stdout of a command in the SAS tokenization stream
 * Handy for capturing output from OS commands such as
 * cd / hostname / uname, etc...
 * unusual use could be as an alternative to %include
 * (e.g. %stdout (cat <filename>) equivalent to %include "filename")
 */
%macro stdout (command, debug=no);

  %local fileref fid rc;

  %let rc = %sysfunc (filename (fileref, &command, PIPE));

%if &debug=yes %then %put fileref=&fileref;

  %if &rc = 0 %then %do;

    %let fid = %sysfunc (fopen (&fileref, S));
    %if &fid ne 0 %then %do;

      %do %while(%sysfunc(fread(&fid)) = 0);

        %local line;
        %let rc = %qsysfunc(fget(&fid,line,200));

%if &debug=yes %then %put line=&line;

        &line

      %end;

      %let fid = %sysfunc (fclose (&fid));
    %end;
    %else %do;

      %put ERROR: PIPE OPEN FAILED, %sysfunc(sysmsg());
      PIPE OPEN FAILED

    %end;

    %let rc = %sysfunc (filename (fileref));
  %end;
  %else %do;
    %put ERROR: COMMAND PIPE SETUP FAILED, rc=&rc..;
    COMMAND PIPE SETUP FAILED
  %end;

%mend;

Sample code

/** /

* A silly example, this is what happens when
* there is no newline concept;
%macro dir();
  %stdout (dir)
%mend;

* A helpful example;
%macro hostname();
  %stdout (hostname)
%mend;


%put %dir;
%put %hostname;

%let hostname = %stdout (hostname);
%put &hostname;
/**/