BookmarkSubscribeRSS Feed
ijmoorejr
Calcite | Level 5

I need some code examples. I am trying to pass the values of a couple of X commands to variables and then use the two values in a if-then/do. I've provided some pseudo code to help explain what I'm trying to accomplish. Thanks.

x 'cd /home/unix/dir/one';
x 'ls -1 | wc -l';

x 'cd /home/unix/dir/two';
x 'ls -1 | wc -l';

if results of first piped command = results of second piped command
then do;
x 'sas runsasprog.sas';
end;
else send email;
4 REPLIES 4
ballardw
Super User

Look in the documentation for FILENAME PIPE and examples to create data sets of the results. Then compare the contents of the datasets. Since I am not sure what you might be considering for "equality" between the output of the commands since creation and or modification datetimes are very likely to be different I am not going to attempt the comparison.

ijmoorejr
Calcite | Level 5

The command 'ls -1 | wc -l' returns the number of files in a given directory. It will simply return a number without timestamps or anything like that. The object is to compare the number/count of files of one directory to another.

Tom
Super User Tom
Super User

Don't use X command. Use PIPE engine instead.

So read the two numbers into two variables and compare number. 

You could for example put the result into a macro variable that you could use to drive what to do next.

data _null_;
   infile 
'cd /home/unix/dir/one
;ls -1 | wc -l
;cd /home/unix/dir/two
;ls -1 | wc -l
' pipe truncover;
  input x y ;
  call symputx('match',x=y);
run;
s_lassen
Meteorite | Level 14

I would not use an X command or pipe for that. SAS has very good functions for directories. One possibility is to use them in to create a macro function:

%macro dnum(path);
  %local fileref rc did memcount;
  /* Fileref is empty, so we assign a temporary, unique fileref */
  %let rc=%sysfunc(filename(fileref, &path));
  %let did=%sysfunc(dopen(&fileref));
  %let memcount=%sysfunc(dnum(&did));
  /* Close directory handle */
  %let rc=%sysfunc(dclose(&did));
  /* deassign fileref */
  %let fileref=%sysfunc(filename(fileref));
&memcount
%mend;

Then you can just use that macro function to get the number of entries in each directory, like

data _null_;
  file outbox <email options>; /* this is the email fileref */
  if %dnum(/home/unix/dir/one)=%dnum(/home/unix/dir/two) then do;
    call system('sas runsasprog.sas');
    put '!EM_ABORT!'; /* Don't send no email */
    end;
  else … /* write something in the email message */

 

 

sas-innovate-white.png

Register Today!

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.

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
  • 4 replies
  • 3928 views
  • 0 likes
  • 4 in conversation