DATA Step, Macro, Functions and more

Pass datstep variable to X command

Accepted Solution Solved
Reply
Contributor
Posts: 40
Accepted Solution

Pass datstep variable to X command

[ Edited ]

Hi

 

Below is the code i am using . I want to execute a job using X command in a datastep.

 

Path and Extn are macro variables. jobname is a datastep variable(from check dataset)

 jobname is a variable which contains list of jobnames (like J_ABC,J_DEF etc) These are located under a mentioned location and extension is sh.Purpose is to execute those jobs using X command. 

 

data _null_;

set check;

%let path=/app/scripts_p2/jobs;
%let extn=sh;

 

 x "&path/jobname.&extn";

 

Kindly advise

 

Its getting retrieved as 

 

/bin/ksh: /app/scripts_p2/jobs/jobname.sh: not found

 

 

Thanks,
Sandhya S


Accepted Solutions
Solution
‎11-23-2017 05:30 AM
Occasional Contributor
Posts: 14

Re: Pass datstep variable to X command

The X-statement is a "global" command that is not part of the datastep. You cannot get this to work. To do this you have to use the system function or Call routine.    
Like   rc = system( cats("&path/",jobname,".&extn") );

View solution in original post


All Replies
Trusted Advisor
Posts: 1,848

Re: Pass datstep variable to X command

1) Have checked that /app/scripts_p2/jobs/jobname.sh file realy exist ?

2) Is the file executable ? - check unix permissions: user's and group's permission.

Contributor
Posts: 40

Re: Pass datstep variable to X command

Hi Shumel. Ok, I think my post is not clear. jobname is a variable which contains list of jobnames (like J_ABC,J_DEF etc) These are located under a mentioned location and extension is sh.
Trusted Advisor
Posts: 1,848

Re: Pass datstep variable to X command

Then of course it cannot work as the result has unix sytax error:

      /app/scripts_p2/jobs/ J_ABC,J_DEF.sh

 

You need to execute each job separatly.

Use SCAN funtion in a loop and execute the jobs one by one.

Super User
Posts: 24,012

Re: Pass datstep variable to X command

Within a data step, Build a string variable that has the code you want to execute, not Including the X. You can use the CATT function with macro variables and data step variables. Then use CALL SYSTEM to have it executed. 

 

Data demo;
Set SASHELP.class;
Str = catt('string1', ....., “&path1.”, job);
*Call system(Str);
Run;

Untested. 

Super User
Posts: 24,012

Re: Pass datstep variable to X command


Sandy10 wrote:

Hi

 

Below is the code i am using . I want to execute a job using X command in a datastep.

 

Path and Extn are macro variables. jobname is a datastep variable(from check dataset)

 jobname is a variable which contains list of jobnames (like J_ABC,J_DEF etc) These are located under a mentioned location and extension is sh.Purpose is to execute those jobs using X command. 

 

data _null_;

set check;

%let path=/app/scripts_p2/jobs;
%let extn=sh;

 

 x "&path/jobname.&extn";

 

Kindly advise

 

Its getting retrieved as 

 

/bin/ksh: /app/scripts_p2/jobs/jobname.sh: not found

 

 

Thanks,
Sandhya S


 

Solution
‎11-23-2017 05:30 AM
Occasional Contributor
Posts: 14

Re: Pass datstep variable to X command

The X-statement is a "global" command that is not part of the datastep. You cannot get this to work. To do this you have to use the system function or Call routine.    
Like   rc = system( cats("&path/",jobname,".&extn") );

Super User
Posts: 10,574

Re: Pass datstep variable to X command

[ Edited ]

This code

data _null_;

set check;

%let path=/app/scripts_p2/jobs;
%let extn=sh;

 

 x "&path/jobname.&extn";

cant't work because X is a global statement that is executed as soon as it is encountered in the program text, so it will ALWAYS resolve to

x "/app/scripts_p2/jobs/jobname.sh";

with "jobname" being a literal. Use call system() instead, which is a data step subroutine:

%let path=/app/scripts_p2/jobs;
%let extn=sh;

data _null_;
set check;
call system("&path./" !! trim(jobname !! ".&extn.");
run;

Also note that it makes your code easier to grok when the macro statements are placed before the data step, because that is the order in which they are executed anyway.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 298 views
  • 0 likes
  • 5 in conversation