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;
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?
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 ????
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.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.