08-19-2017 06:35 PM
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 &";
08-20-2017 02:58 AM
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
to your command, to reroute stderr to stdout.
08-20-2017 08:51 PM - edited 08-20-2017 08:55 PM
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;
08-21-2017 02:03 AM
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.
08-22-2017 01:46 AM
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"
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:
08-20-2017 08:39 PM
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.