I need a system lookup to get the uid of a Unix userid. Using macro variables, this works:
%let userid = sasbatch ;
filename tmp pipe "id -u &userid" ;
data _null_ ;
infile tmp ;
input uid ;
put uid= ;
call symput('uid',uid) ;
run ;
%put &=uid ;
2 data steps needed without macro variables :
data _null_ ;
userid = 'sasbatch' ;
command = cat('id -u ',userid, ' > /tmp/uid_out.txt' ) ;
put command= ; /* id -u sasbatch > /tmp/uid_out.txt as expected */
call system(command) ;
%put %sysfunc(sysrc()) - %sysfunc(sysmsg()) ; /* Nothing useful */
/* Cannot be opened within the same step
filename tmpin '/tmp/uid_out.txt' ;
infile tmpin ;
input uid ;
put uid= ;
*/
run ;
data _null_ ;
filename tmpin '/tmp/uid_out.txt' ;
infile tmpin ;
input uid ;
put uid= ; /* 506 as expected */
run ;
Is there a single data step solution?
Without '> /tmp/uid_out.txt' , the result 506 is displayed in the calling Unix shell as standard output. Is there anyway to read it back in ?
Use a dynamic pipe:
data users;
input username $;
datalines;
sasbatch
;
data uids;
set users;
fvar = catx(" ","id -u",username,'2>&1');
infile dummy pipe filevar=fvar end=done;
do until (done);
input id;
output;
end;
run;
Use a dynamic pipe:
data users;
input username $;
datalines;
sasbatch
;
data uids;
set users;
fvar = catx(" ","id -u",username,'2>&1');
infile dummy pipe filevar=fvar end=done;
do until (done);
input id;
output;
end;
run;
I call this thing the "dynamic pipe". Hopefully, it will be part of a presentation at SAS Global Forum 2022 in San Diego.
You will have noticed the "2>&1" I added to the external command. It instructs the system to reroute stderr (error output) to stdout (the standard output stream), so that SAS catches it. You can use extra logic to detect anything abnormal in the response, by looking for unexpected strings/characters in _INFILE_.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.