BookmarkSubscribeRSS Feed
PratapDeo
Calcite | Level 5

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.

9 REPLIES 9
Quentin
Super User

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.

The Boston Area SAS Users Group (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.
PratapDeo
Calcite | Level 5
I am using %INCLUDE statement to run code B.
PratapDeo
Calcite | Level 5

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.

Kurt_Bremser
Super User

@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.

Tom
Super User Tom
Super User

@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)
PratapDeo
Calcite | Level 5

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. 

 

Tom
Super User Tom
Super User

@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)

 

Ron_MacroMaven
Lapis Lazuli | Level 10

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

 

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 9 replies
  • 3873 views
  • 3 likes
  • 5 in conversation