Hi guys! Could you please give me some help for my urgent question on setting a macro variable? I will appreciate it very much for your warm help!
I need to set a macro variable 'indep = a + b1* mkt + b2 * smb + b3 * hml' and use it in the following proc model step. However, it doesn't work. How can I set a macro variable which has '+' signs in this situation? Your answer will help me a lot!
proc model data=ret_5;
by id;
parms a b1 b2 b3;
instruments mkt smb hml;
exret = &indep;
fit exret / gmm kernel=(bart, 2, 0);
ods output parameterestimates=cs;
quit;
Alternatively you may prefer next code:
%let formula = a + b1* mkt + b2 * smb + b3 * hml;
%macro p_model(var,tst,dsn=ret_5);
proc model data=&dsn;
by id;
parms a b1 b2 b3;
instruments mkt smb hml;
exret = &var = &tst ;
fit exret / gmm kernel=(bart, 2, 0);
ods output parameterestimates=cs;
quit;
%mend p_model;
%p_model(indep,&formula); /* input = ret_5 the default dataset */
%p_model(anyvar,&formula );
%p_model(new_var, <new_formula>, dsn=<any_dataset>); /* any other input */
Have you tried to precede proc model by next statement:
%let indep = a + b1* mkt + b2 * smb + b3 * hml;
thus assigning the value to the macro variable.
if positive, then explain what do yo mean by "it doesn't work." ?
In such case better post the log with your explanatin.
Hi! The log informs me to define the variable preceding proc model. But I need to change the 'indep' with other variables in this macro each time. If I use the %let statement in the macro, it seems I have to change this variable in the macro. Can I just change it each time with other macro variables together when calling this macro? Many thanks for your help!
Would you prefer the code:
%let test = a + b1* mkt + b2 * smb + b3 * hml;
proc model data=ret_5;
by id;
parms a b1 b2 b3;
instruments mkt smb hml;
exret = indep = &test; /* any time replace the indep into wanted variable */
fit exret / gmm kernel=(bart, 2, 0);
ods output parameterestimates=cs;
quit;
Hi~Thanks for your reply! But it seems I still need to change the test variable each time in the macro, right?
Alternatively you may prefer next code:
%let formula = a + b1* mkt + b2 * smb + b3 * hml;
%macro p_model(var,tst,dsn=ret_5);
proc model data=&dsn;
by id;
parms a b1 b2 b3;
instruments mkt smb hml;
exret = &var = &tst ;
fit exret / gmm kernel=(bart, 2, 0);
ods output parameterestimates=cs;
quit;
%mend p_model;
%p_model(indep,&formula); /* input = ret_5 the default dataset */
%p_model(anyvar,&formula );
%p_model(new_var, <new_formula>, dsn=<any_dataset>); /* any other input */
Thanks! Could you please give me a little more explanations? I don't understand what the meaning of 'exret = &var = &formula' is. What's more, when using ¯o (indep=), it seems I have already define a macro variable indep and I can directly call indep by using %macro (indep= a + b1* mkt + b2 * smb + b3 * hml);, why I need to define the variable again?
A short explanation to macro programming:
1) Assigning a value to a macro variable is done by:
%LET <macro_variable_name> = any string;
2) Defining a macro program is done by:
%MACRO <macro_program_name>(arguments seaparated by comma); ... sas code to run ... ... using &<macro_variable> or &<argument> to be replaced at run time by their assigned value ......... %MEND [<macro_program_name>]; /* closes the macro program */ %<macro_program_name>( assigned arguments); /* execute the macro program */
3) There are two kinds of arguments that can be defined with a macro program:
3.1) positional arguments must be given in same order as defined in the macro program.
3.2) named arguments must follow after all positional arguments.
You can assign a default value to named argument only, and
you can override the default value at invoking the macro program to be executed;
There is much more to learn about macro language .
Welcome to a new sas field.
The log reports: All positional parameter variables in the %MACRO statement must be listed before any keyword parameter variable is listed
Hi! I have revised my code based on your suggestion and it works now! Thanks soooo much!
Really, you need to show how you build your macro variable INDEP
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!
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.