BookmarkSubscribeRSS Feed
srdhoble
Calcite | Level 5

Hi Team,

               Please help me on this.

I have a SAS BASE code.This code contains code for 3 models.I want to have a macro variable in beginning which has parameters to set models for which you want to execute for.

for example if a give call like %run(model1,model2)

 

it executes just part of code related to model1 and model2.

Please guide me how to go about it.

 

11 REPLIES 11
Cynthia_sas
SAS Super FREQ
Hi: This paper has a good introduction to the SAS Macro facility, including an example of conditionally executing code:
https://support.sas.com/resources/papers/proceedings13/120-2013.pdf
cynthia
srdhoble
Calcite | Level 5

Hi,,

 Here is my requirement.

 

my script contains code for.

Model1

Model2

Model3

 

i need a way to run one or more models ast a time..

 

so my macro calls could be

 

%run_model(Model1)

%run(Model1,MOdel2)

%run(Model2,Model3)

%run(Model1,Model2,Model3)

 

 

Tom
Super User Tom
Super User

Write a macro and use conditional statements to execute the parts you want.

From your description I would have one parameter and pass in the list of models to run.

%macro run(model_list);
%let model_list=%upcase(model_list);

%if %sysfunc(indexw(&model_list,MODEL1)) %then %do;
* code to run model 1 goes here;
%end;

%if %sysfunc(indexw(&model_list,MODEL2)) %then %do;
* code to run model 2 goes here;
%end;

%* add as many models as you want ;

%mend run;

You can then call it with 0,1,2,3,.... models by just listing the model names.

%run(model1)
%run(model1 model2)

 

srdhoble
Calcite | Level 5

Hi,,

 Here is my requirement.

 

my script contains code for.

Model1

Model2

Model3

 

i need a way to run one or more models ast a time..

 

so my macro calls could be

 

%run_model(Model1)

%run(Model1,MOdel2)

%run(Model2,Model3)

%run(Model1,Model2,Model3)

Tom
Super User Tom
Super User

Don't use commas in the macro call. That implies multiple paramaters. Use spaces so that the list of models is passed as the value to a single parameter.

srdhoble
Calcite | Level 5

hi,Below is log which is getting generated,the macro call is unable to execute code as %IF condition is comming false.Please Help me on it.Thanks!

 

 options mprint mlogic;
 %runmodels(alpha beta);
MLOGIC(RUNMODELS): Beginning execution.
MLOGIC(RUNMODELS): Parameter MODEL_LIST has value alpha beta
MLOGIC(RUNMODELS): %LET (variable name is MODEL_LIST)
MLOGIC(RUNMODELS): %IF condition %sysfunc(indexw(&model_list,alpha)) is FALSE
MLOGIC(RUNMODELS): %IF condition %sysfunc(indexw(&model_list,beta)) is FALSE
MLOGIC(RUNMODELS): Ending execution.

Tom
Super User Tom
Super User

Your log messages do not show what value the %LET statement assigned to the MODEL_LIST variable, but if it was like the macro I posted before it would have converted it to upper case to allow the testing to work when users passed in lowercase or mixed case values.

 

But the  %IF conditions that are resulting in false are checking for lowercase alpha and beta.

 

Either change the %LET or change the INDEXW() tests.

 

srdhoble
Calcite | Level 5

Hi I got it working..I have another question related to it.

 

Now below is my macro

%macro run(model_list);

%if %sysfunc(indexw(&model_list,ALPHA)) %then %do;

%if %sysfunc(indexw(&model_list,BETA)) %then %do;

--

--

--;

%mend run;

 

and then the macro call
%run(ALPHA BETA)

 

so i have to run the macro defination first..scroll down the entire script and then call macro..

is there somewhy i can do both these steps of running macro defination and call in one step?

 

TIA. 🙂

 

 

 

srdhoble
Calcite | Level 5

SO basically is there a way i can run macro defination and macro call together.?

Tom
Super User Tom
Super User

I don't understand the question.  If you have a program file then just run the program.

%macro run(list);
....
%mend run;;
%run(alpha);
%run(beta);

If you want to maintain the macro defintion and the program that use it as two different source files then just add a %INCLUDE to include the macro definition when needed.  

%include 'run.sas' ;
%run(apha);
%run(beta);

... .more code ...
%run(beta alpha);

Note that once the macro is compiled you do not need to re-run the definition unless you restart SAS or delete the compiled version somehow.  

 

You could also look into creating your own autocall library. With AUTOCALL SAS will load the macro definition the first time you try to run it so you don't have to add %include statements for every macro you want to call.

 

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 11 replies
  • 4391 views
  • 2 likes
  • 4 in conversation