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_.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.