Help using Base SAS procedures

Pass parameter from one SAS code to another SAS code in Linux.

Reply
Occasional Contributor
Posts: 15

Pass parameter from one SAS code to another SAS code in Linux.

Hi,

I have to execute SAS Code B from SAS Code A during run time. Code B will be executed by taking a parameter from Code A. Please suggest me how we can pass parameter from Code A to Code B in Linux.

 

Many Thanks.

PROC Star
Posts: 1,271

Re: Pass parameter from one SAS code to another SAS code in Linux.

How are you planning to run Code B?  Are you going to use an %INCLUDE statement? Are yo going to use the X statement or similar to start a new SAS that runs B?  Depending on how you will run B, there are different methods for passing parameters to it.

Occasional Contributor
Posts: 15

Re: Pass parameter from one SAS code to another SAS code in Linux.

I am using %INCLUDE statement to run code B.
Super User
Posts: 7,124

Re: Pass parameter from one SAS code to another SAS code in Linux.


PratapDeo wrote:
I am using %INCLUDE statement to run code B.

Then set a macro variable in A before the %include, and use it in B.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 15

Re: Pass parameter from one SAS code to another SAS code in Linux.

[ Edited ]

I have to set three parameters 'US, 'DIRECT' and 'INDIRECT' in code A and use it to execute three different codes. In the code A itself we are calling and executing three different codes based on three parameters mentioned earlier.

Could you please help how we can set macros.

Super User
Posts: 7,124

Re: Pass parameter from one SAS code to another SAS code in Linux.


PratapDeo wrote:

I have to set three parameters 'US, 'DIRECT' and 'INDIRECT' in code A and use it to execute three different codes. In the code A itself we are calling and executing three different codes based on three parameters mentioned earlier.

Could you please help how we can set macros.


Macro variables are set with %let.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Super User
Posts: 6,718

Re: Pass parameter from one SAS code to another SAS code in Linux.

[ Edited ]

PratapDeo wrote:

I have to set three parameters 'US, 'DIRECT' and 'INDIRECT' in code A and use it to execute three different codes. In the code A itself we are calling and executing three different codes based on three parameters mentioned earlier.

Could you please help how we can set macros.


Did you mean three parameters? So perhaps something like this?

%let us=1;
%let direct=0;
%let indirect=0;
%include 'b.sas';

Then inside B you can either use macro logic or other code to test the the value of US to see if you want to run the US part of the code?

%macro inside_b ;
  %if &us %then %do;
    *** code to process US data ***;
  %end;
  ...
%mend inside_b;
%inside_b;

But perhaps you really wanted to have a single parameter that could take one of three values?

%let which=US;

Then inside B  

%macro inside_b ;
  %if (&which = US) %then %do;
    *** code to process US data ***;
  %end;
  ...
%mend inside_b;
%inside_b;

But perhaps you really wanted to have a single parameter that could take any combination of three values?

%let which=US DIRECT;

Then inside B  

%macro inside_b  ;
  %if %sysfunc(indexw(US,&which)) %then %do;
    *** code to process US data ***;
  %end;
  ...
%mend inside_b;
%inside_b;

 Or pehaps you want to make all of B into a macro and then just call it from inside A?  In that case you might have B look like.

%macro b(which)  ;
  %if %sysfunc(indexw(US,&which)) %then %do;
    *** code to process US data ***;
  %end;
  ...
%mend b;

And then A will look like:

%* Compile the B macro ;
%include 'b.sas';
%* Call the B macro ;
%b(US)
Occasional Contributor
Posts: 15

Re: Pass parameter from one SAS code to another SAS code in Linux.

[ Edited ]

Many thanks Tom for your response. I have one doubt, in the sample code you have mentioned as 

%let us=1;
%let direct=0;
%let indirect=0;

 

while runing my code it is picking the condition which is satisfying the first case i.e. US, and same code is getting replicated for direct and indirect also. (It seems values for Direct and Indirect is given as Zero, is that the reason? )whereas I have to pick for all thre conditions i.e. US, Direct, Indirect.. Could you please help me with the proper value. 

 

Super User
Super User
Posts: 6,718

Re: Pass parameter from one SAS code to another SAS code in Linux.


PratapDeo wrote:

Many thanks Tom for your response. I have one doubt, in the sample code you have mentioned as 

%let us=1;
%let direct=0;
%let indirect=0;

 

while runing my code it is picking the condition which is satisfying the first case i.e. US, whereas I have to pick for all thre conditions i.e. US, Direct, Indirect.. Could you please help me with the proper value. 

 


Not sure what the quesiton is. Do you have one macro variable which can take multiple vlaues?  Normally if the options are single words like in your case I would use a space delimited list as the user's choice of combinations they want.

%let choice=US ;
%let choice=US DIRECT ;
%let choice=INDIRECT ;

Then in the macro code I would have blocks of code that test if the keyword is in the user supplied list.

%if %sysfunc(indexw(&choice,US)) %then ;
* generate report for US option ;
....
%end;
%if %sysfunc(indexw(&choice,DIRECT)) %then ;
* generate report for DIRECT option ;
....
%end;
%if %sysfunc(indexw(&choice,INDIRECT)) %then ;
* generate report for INDIRECT option ;
....
%end;

If you want to have three separate parameters then I like to code them as boolean choice.  So 1 means yes I want that and 0 means no I don't.  If you are building an user interface then they they would just have push buttons or check boxes for the use to indicate whether to use them. So ther result might look like what I posted before.

%let us=1;
%let direct=0;
%let indirect=0;

and then the code in the maceo will reference these three different parameters.

%if &US %then ;
* generate report for US option ;
....
%end;
%if &direct %then ;
* generate report for DIRECT option ;
....
%end;
%if &indirect %then ;
* generate report for INDIRECT option ;
....
%end;

Normally if the macro is getting its input from user entered text I would also add calls to the %PARMV() utiltity at the top of the macro to check that they have supplied valid values and cleanup the case of the input so that the rest of the macro doesn't have to worry about user entry errors.  You can find the code for %PARMV() macro on Github https://github.com/sasutils/macros/blob/master/parmv.sas

 

So in the first case the %parmv() call would look like this to test that the words in CHOICE come from the list of valid values. It will also make the CHOICE into uppercase. The _WORDS=1 option indicates that the use can include multiple values in the parameter.

%parmv(choice,_words=1,_val=us direct indirect)

For the second version the PARMV calls would looke like this. _VAL=0 1 means that it will allow user to enter things like YES, NO, TRUE, FALSE and %parmv() will accept those as valid values and also convert them into the appropriate 0 or 1 value.

%parmv(us,_val=0 1)
%parmv(direct,_val=0 1)
%parmv(indirect,_val=0 1)

 

Regular Contributor
Posts: 226

Re: Pass parameter from one SAS code to another SAS code in Linux.

I see a reply that explains how to use either %includes or macros.

 

Here is a page which explains parameters with %includes

 

sco: Parameterized_include_file

 

the basic idea is to allocate macro variables in the global symbol table

and reference them in the %include file.

 

this method doesn't allow make it easy to do conditional processing in the subroutine

but it can be done with %sysfunc and ifc

 

sco: Using_Functions_SYSFUNC_and_IFC_to_conditionally_execute_statements_in_open_code

 

and (%do) loops cannot be done without, for instance, call execute

 

Ron Fehd %includes or macro, whichever

 

 

Ask a Question
Discussion stats
  • 9 replies
  • 269 views
  • 3 likes
  • 5 in conversation