How to Run Shell Script inside SAS?

Reply
Contributor
Posts: 34

How to Run Shell Script inside SAS?

Hello,

I am trying to run a .sh file from sas. I tried the following

data _null_;
call system ('cd /users/smith/report.sh');
run;

But it is not executing my script .

Please help 

Valued Guide
Posts: 947

Re: How to Run Shell Script inside SAS?

  1. "cd" is change directory.  It will not run the script.
  2. Have you tried the X statement?  It's a global statement (i.e. does not need to be (should not be) inside a DATA step or other PROC step.   See http://www.sascommunity.org/wiki/X_statement

 

  x '/users/smith/report.sh' ;

 

You also need to consider whether you want your SAS program to wait for the X statements complete or not.  If yes, then issue

  options xwait;

somewhere prior to the X statement(s).

 

If not

  options noxwait;

 

 

Note many sysadmins turn off the ability to issue X statements when configuring SAS.

Super User
Posts: 7,444

Re: How to Run Shell Script inside SAS?

As @mkeintz already noted, cd won't run anything, it just (in UNIX) sets the current working directory of the shell (or, when submitted with x or call system(), the cwd of the SAS session).

The best method for executing external programs is with a filename pipe:

filename oscmd pipe "/users/smith/report.sh 2>&1";

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

filename oscmd clear;

This will catch all output (including error messages from stderr, that's done with the 2>&1 redirection) from the external program in the SAS log.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super Contributor
Posts: 434

Re: How to Run Shell Script inside SAS?

Could you please tell me why we use

2>&1?

 

Super User
Posts: 7,444

Re: How to Run Shell Script inside SAS?


Babloo wrote:

Could you please tell me why we use

2>&1?

 


You should start to read all of a post before answering/questioning:

Quote from my post:

(including error messages from stderr, that's done with the 2>&1 redirection)

A google search for "2>&1" will give you extensive information for why it is done.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Ask a Question
Discussion stats
  • 4 replies
  • 213 views
  • 2 likes
  • 4 in conversation