SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
GeorgeSAS
Lapis Lazuli | Level 10

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!

9 REPLIES 9
Reeza
Super User
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.
SuryaKiran
Meteorite | Level 14

Hi,

 

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

 

Thanks,
Suryakiran
Kurt_Bremser
Super User

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.

GeorgeSAS
Lapis Lazuli | Level 10

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!

 

Kurt_Bremser
Super User

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.

GeorgeSAS
Lapis Lazuli | Level 10

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!

Kurt_Bremser
Super User

@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
GeorgeSAS
Lapis Lazuli | Level 10

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

ChrisBrooks
Ammonite | Level 13

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.

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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
  • 9 replies
  • 9026 views
  • 4 likes
  • 5 in conversation