DATA Step, Macro, Functions and more

execute a shell script from unix sas

Reply
Regular Contributor
Posts: 241

execute a shell script from unix sas

Hello everyone,

 

I run SAS code in UNIX. I have a shell script file( sortdata.sh ) need to be called from this SAS code.

 

May I ask how to coding it in detail in SAS to execute the .sh file? using data _null_ step? or ..?

 

There maybe different methods can do this, any method will be greatly appreciated!

 

 

code maybe used:

a) X command
X 'sh /SASInt/.../sortdata.sh &';




b) %macro sample;
%sysexec %str(sh /SASInt/.../sortdata.sh &)'






c) filename results pipe "sh /SASInt/.../sortdata.sh &";

 

 

Thanks!

Super User
Posts: 19,878

Re: execute a shell script from unix sas

Posted in reply to GeorgeSAS
Figure out the command required on UNIX first.
Then add the X or SYSEXEC in SAS. You do need to make sure you have privileges to do this as well.
Frequent Contributor
Posts: 141

Re: execute a shell script from unix sas

Posted in reply to GeorgeSAS

Hi,

 

Before you run the .sh file, you need to make it executable. Use X commands to run UNIX commands

 

Thanks,
Suryakiran
Super User
Posts: 7,866

Re: execute a shell script from unix sas

Posted in reply to GeorgeSAS

I prefer the filename pipe method, as it enables me to catch all system output back into the SAS log. Don't forget to add

2>&1

to your command, to reroute stderr to stdout.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Regular Contributor
Posts: 241

Re: execute a shell script from unix sas

[ Edited ]
Posted in reply to KurtBremser

Thank you Kurt!

 

Would you please tell me where to put the "2>&1" ? 

 

filename results pipe "sh sortdata.sh &" 2>&1 ; /*this not work, do I need to put it in sh file?*/
data pip;
infile results pad missover lrecl=3;
input answer $50.;
put answer=;
run;
proc print _last_;
run;

Thanks!

 

Super User
Posts: 7,866

Re: execute a shell script from unix sas

Posted in reply to GeorgeSAS

Yes, the redirection is part of the UNIX command.

The command should be

"sh sortdata.sh 2>&1"

Note that I also omitted your & (run job in background), as that would be counterproductive in catching all output.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Regular Contributor
Posts: 241

Re: execute a shell script from unix sas

Posted in reply to KurtBremser

Thank you Kurt,

 

Does it must be a .sh file to use 2>&1 ?

 

if it is a sas file, I can't use this :

"sas sortdata.sas 2>&1"

 thanks!

Super User
Posts: 7,866

Re: execute a shell script from unix sas

Posted in reply to GeorgeSAS

GeorgeSAS wrote:

Thank you Kurt,

 

Does it must be a .sh file to use 2>&1 ?

 

if it is a sas file, I can't use this :

"sas sortdata.sas 2>&1"

 thanks!


Can be anything runnable from the commandline.

I wrote a quick program:

data sasuser.test;
x1 = datetime();
format x1 e8601dt22.;
put x1=;
run;

and stored it as test.sas in my home directory.

Then I ran this from my EG session:

filename oscmd pipe "/sas/SASFoundation/9.4/sas $HOME/test.sas -altlog $HOME/test.log 2>&1";

data _null_;
infile oscmd;
input;
put _infile_;
run;

Note that I specified the whole path to the SAS executable, as we do not have that in the PATH environment variable.

The log in EG shows this:

24         filename oscmd pipe "/sas/SASFoundation/9.4/sas $HOME/test.sas -altlog $HOME/test.log 2>&1";
25         
26         data _null_;
27         infile oscmd;
28         input;
29         put _infile_;
30         run;

NOTE: The infile OSCMD is:
      Pipe-Kommando="/sas/SASFoundation/9.4/sas $HOME/test.sas -altlog $HOME/test.log 2>&1"

NOTE: 0 records were read from the infile OSCMD.
NOTE: DATA statement used (Total process time):
      real time           0.41 seconds
      cpu time            0.00 seconds

which indicates no message came back from the system.

$HOME/test.log looks like this (excerpt):

1          data sasuser.test;
2          x1 = datetime();
3          format x1 e8601dt22.;
4          put x1=;
5          run;

x1=2017-08-22T07:39:14
NOTE: The data set SASUSER.TEST has 1 observations and 1 variables.
NOTE: DATA statement used (Total process time):
      real time           0.03 seconds
      cpu time            0.00 seconds

and dataset SASUSER.TEST shows this:

x1

   2017-08-22T07:39:14
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Regular Contributor
Posts: 241

Re: execute a shell script from unix sas

Posted in reply to GeorgeSAS

data _null_;
call system ('cd /..../test');
call system ('sas test123.sas &');
run;

Super Contributor
Posts: 441

Re: execute a shell script from unix sas

Posted in reply to GeorgeSAS

You can put both commands in the same call system statement separated by a semi-colon as shown on this page http://support.sas.com/documentation/cdl/en/hostunx/69602/HTML/default/viewer.htm#p0w085btd5r0a4n1km... - you'll have to use a macro quoting function but at least that way you can be sure all commands execute inside the same UNIX shell.

Ask a Question
Discussion stats
  • 9 replies
  • 248 views
  • 4 likes
  • 5 in conversation