BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
xizidememeda
Calcite | Level 5

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;
1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

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 */

View solution in original post

10 REPLIES 10
Shmuel
Garnet | Level 18

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.

xizidememeda
Calcite | Level 5

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! 

Shmuel
Garnet | Level 18

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;
xizidememeda
Calcite | Level 5

Hi~Thanks for your reply! But it seems I still need to change the test variable each time in the macro, right?

Shmuel
Garnet | Level 18

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 */
xizidememeda
Calcite | Level 5

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 &macro (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?

 

Shmuel
Garnet | Level 18

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.

xizidememeda
Calcite | Level 5

The log reports: All positional parameter variables in the %MACRO statement must be listed before any keyword parameter variable is listed

xizidememeda
Calcite | Level 5

Hi! I have revised my code based on your suggestion and it works now! Thanks soooo much!

ballardw
Super User

Really, you need to show how you build your macro variable INDEP

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 10 replies
  • 744 views
  • 0 likes
  • 3 in conversation