BookmarkSubscribeRSS Feed
alepage
Barite | Level 11

Few days ago, someone from the community propose me the script below to send multiple unix command to our unix server.

It works well.  However the purpose of my request today is could someone explain me in details how does it works because it is not clear in my mind.

Please note that the filename is filelisting, the variable unixcommand contains the unix commands to be sent to the unix server.

Could you please explain each step of this piece of code

 

 

data _null_; 
  set filelisting;
  infile unix pipe filevar=unixcommand end=eof;
  do while (not eof);
    input;
    put _infile_;
  end;
run;
3 REPLIES 3
Tom
Super User Tom
Super User

Do you know what a PIPE is in Unix? 

https://www.geeksforgeeks.org/piping-in-unix-or-linux/

 

Do you know what the PIPE file device type is in SAS? Look at FILENAME statement.

Do you know what the FILEVAR= option of the INFILE statement does?

Do you know what the END= option of the INFILE statement does?

Do you know what the WHILE () option on a DO statement does?

Do you know what INPUT does?

Do you know what the _INFILE_ automatic variable is?

 

alepage
Barite | Level 11
data _null_; 
  set filelisting;
  infile unix pipe filevar=unixcommand end=eof;
  do while (not eof);
    input;
    put _infile_;
  end;
run;

 I might be wrong but what I think, is:

 

set filelisting; is use to select the dataset that I want to use

do while (not eof);

...

end

will execute what at in between until the end of the file.

 

input statement read the file filelisting one record at the time

which is put into _infile_;

 

infile unix pipe ????

Tom
Super User Tom
Super User

Close.

 

The SET statement reads the next observation from the input dataset.

The INFILE statement tells SAS where to read data from when you run an INPUT statement.  This one says to use the PIPE device type and use the variable referenced in the FILEVAR= option as the name of the file to read.  But since you are using the PIPE device it is not really the name of a file, but instead it is the command to by run whose piped output is what will be read.  The END= option says to use the variable named EOF as the one that will be set TRUE when you have read past the end of this file.

The DO WHILE() will then iterate as long as there is more output coming from the command.

The INPUT statement is used to read in from a text file.  When you read a line SAS will populate the _INFILE_ automatic variable with the content of that line.  So an INPUT statement with no other options just populates that _INFILE_ variable without modifying any other variables.

The PUT statement writes text to the LOG (or what ever file you have opened with the most recent FILE statement).  In this case it is just going to write the _INFILE_ automatic variables.  So the DO loop writes all (any) output that the Unix command generated to the SAS log.  Note that a lot of commands will not write any output.

 

The data step will execute until it stops.  This particular one will stop when the SET statement reads past the end of its input SAS dataset.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 447 views
  • 2 likes
  • 2 in conversation