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

Hello Team ,

 

am looking forward to achieve below through macro 

Scenario : 

      if status = 3 and run_time between (19 hrs to next day 5 hrs)

                   then

              check the whether .sas program is running in Unix or not

                  and the result of Ps command stored in users home directory temp1.txt

                   x '(ps -ef | grep test_program.sas | grep -v grep)>/home/temp1.txt';

                 calculate the length of file and store the return value from unix in a variable through below command

                  %let file_length = x '(stat -c %s /home/temp1.txt)';

         

                 if file_length=0 then %put the program is not executed ;

                  else then %put the program is still running ;

       end if 

 

Question : 

1. how to store the current time in a variable ?

2. How to store the return value of unix command in a macro variable 

 

Please help with your suggestions on this

 

1 ACCEPTED SOLUTION

Accepted Solutions
CarmineVerrell
SAS Employee

So based on your example, I would highly recommend you take some SAS training course. More specifically the SAS Macro Language 1: Essentials course(please note that the prerequisite for this course is the SAS Programming 2: Data Manipulation Techniques).

 

The macro course is two days of training and will help you understand that you are mixing compile time statements and execution time statements in your example, which means that it will never work.  

 

So to answer you First question, "getting the result of Ps command stored in users home directory temp1.txt and storing that value in a macro" here is how i would handle this.

You can try using the data step to read the text file and use call symputx to create a macro variable. 

 

Here is an example using the pipe command(you need to have x commands enabled for this to work on a sas server).

filename filelist pipe 'stat -c %s /home/temp1.txt ';
data _null_;
   infile filelist truncover;
   input filename $100.;
    call symputx("newMacro",filename); /*This puts the entire piped contents into the macro, i think you might                                                               want to use a sas function to filter this value*/

run;
run;

 

 

Here is how you would store the current time in a macro variable;

%let time=%sysfunc(time(),time16.);

 

Hope this helps.

 

Carmine

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

Do you want the text the command writes?

Use the PIPE filename engine.

data result;
  infile 'my unix command' pipe truncover;
  input line $200.;
run;

You could add a CALL SYMPUTX() if you wanted the result in a macro variable instead of a real variable in a real dataset.

CarmineVerrell
SAS Employee

So based on your example, I would highly recommend you take some SAS training course. More specifically the SAS Macro Language 1: Essentials course(please note that the prerequisite for this course is the SAS Programming 2: Data Manipulation Techniques).

 

The macro course is two days of training and will help you understand that you are mixing compile time statements and execution time statements in your example, which means that it will never work.  

 

So to answer you First question, "getting the result of Ps command stored in users home directory temp1.txt and storing that value in a macro" here is how i would handle this.

You can try using the data step to read the text file and use call symputx to create a macro variable. 

 

Here is an example using the pipe command(you need to have x commands enabled for this to work on a sas server).

filename filelist pipe 'stat -c %s /home/temp1.txt ';
data _null_;
   infile filelist truncover;
   input filename $100.;
    call symputx("newMacro",filename); /*This puts the entire piped contents into the macro, i think you might                                                               want to use a sas function to filter this value*/

run;
run;

 

 

Here is how you would store the current time in a macro variable;

%let time=%sysfunc(time(),time16.);

 

Hope this helps.

 

Carmine

learn_SAS_23
Quartz | Level 8
Thanks Carmine and Tom for useful tips , it helps to achieve the solution

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 1656 views
  • 2 likes
  • 3 in conversation