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

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!

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.

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
  • 659 views
  • 0 likes
  • 2 in conversation