BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
acfarrer
Quartz | Level 8

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 ?

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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;

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

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;
acfarrer
Quartz | Level 8
Awesome and quick !
I had tried filename pipe but not tried to read the output. Using filevar= for username is very creative .
Thanks very much - I am sure others will find it useful
Kurt_Bremser
Super User

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_.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1287 views
  • 0 likes
  • 2 in conversation