DATA Step, Macro, Functions and more

how to execute part of code

Reply
Contributor
Posts: 20

how to execute part of code

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.

 

SAS Super FREQ
Posts: 8,868

Re: how to execute part of code

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
Super User
Posts: 7,854

Re: how to execute part of code

You will have to post the code and mark the parts that need to be parameterized, then we can give you hints how to go about it.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 20

Re: how to execute part of code

Posted in reply to KurtBremser

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)

 

 

Super User
Super User
Posts: 7,076

Re: how to execute part of code

[ Edited ]

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)

 

Contributor
Posts: 20

Re: how to execute part of code

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)

Super User
Super User
Posts: 7,076

Re: how to execute part of code

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.

Contributor
Posts: 20

Re: how to execute part of code

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.

Super User
Super User
Posts: 7,076

Re: how to execute part of code

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.

 

Contributor
Posts: 20

Re: how to execute part of code

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. Smiley Happy

 

 

 

Contributor
Posts: 20

Re: how to execute part of code

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

Super User
Super User
Posts: 7,076

Re: how to execute part of code

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.

 

 

Ask a Question
Discussion stats
  • 11 replies
  • 505 views
  • 2 likes
  • 4 in conversation