DATA Step, Macro, Functions and more

create dynamic model code using macro?

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 110
Accepted Solution

create dynamic model code using macro?

[ Edited ]

Let us say I have a dataset like so:

 

ID X1 X2
1 1 1
2 1 0

 

Here Id indicates something to be modeled. The 0 and 1 in columns X1 and X2 indicate whether the indepedent varible should be used in the model (0=do not include, 1=include). So the model code to be produced would be:

 

PROC GLMSELECT data=TrainingData;
model DependentVariable
= 
X1
X2
/ selection = stepwise(select=SL choose=AICC);
run;

 

and

 

 

PROC GLMSELECT data=TrainingData;
	model DependentVariable
		= 
		X1
	/ selection = stepwise(select=SL choose=AICC);
run

respectivley. Ideally I would like to invoke a macro given the Id. The Id is then used to write and execute the modeling code. Btw the relvantTrainingData given Id can be easily initialised from anothe dataset. Any ideas how to achieve the above? Thanks!

 

 


Accepted Solutions
Solution
‎10-07-2017 04:31 PM
Super User
Posts: 5,513

Re: create dynamic model code using macro?

Posted in reply to csetzkorn

If you are planning to use CALL EXECUTE, you don't really need to define a macro.  Here's one way:

 

data _null_;

set have;

call execute('PROC GLMSELECT data=TrainingData;') ;

call execute('model Dependentvariable =') ;

array indeps {*} X1 X2 and a bunch more;

do _n_=1 to dim(indeps);

   if indeps{_n_} = 1 then do;

      varname = vname(indeps{_n_});

      call execute(varname);

   end;

end;

call execute ('/ selection = stepwise(select=SL choose=AICC); run;') ;

run;

 

Obviously it's untested, so it might need to be debugged.

View solution in original post


All Replies
Super User
Posts: 19,822

Re: create dynamic model code using macro?

Posted in reply to csetzkorn

1. Create a macro for your regression that takes a variable list

2. In the data step, create a variable that has the values you need, ie 

 

ID X1 X2 Variable_List

1 1 1 X1 X2

2 1 0 X1

 

3. Pass those to the macro using CALL EXECUTE

 

 

 

 

Solution
‎10-07-2017 04:31 PM
Super User
Posts: 5,513

Re: create dynamic model code using macro?

Posted in reply to csetzkorn

If you are planning to use CALL EXECUTE, you don't really need to define a macro.  Here's one way:

 

data _null_;

set have;

call execute('PROC GLMSELECT data=TrainingData;') ;

call execute('model Dependentvariable =') ;

array indeps {*} X1 X2 and a bunch more;

do _n_=1 to dim(indeps);

   if indeps{_n_} = 1 then do;

      varname = vname(indeps{_n_});

      call execute(varname);

   end;

end;

call execute ('/ selection = stepwise(select=SL choose=AICC); run;') ;

run;

 

Obviously it's untested, so it might need to be debugged.

☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 79 views
  • 2 likes
  • 3 in conversation