I am defining a macro variable inside a macro. Then, I am feeding it into a second macro. Inside macro2 counter changes value to 200. However, when I check what is inside the macro variable that I put in after macro 2 runs, it still says 0. I would like it to store the value 200? is this possible?
%macro macro1();
%let variable1= 0;
macro2(counter=&variable1)
%put &variable1;
%mend macro1;
%macro1;
You have a couple of issues here. First of all, you are missing the
%
before your call tomacro2
, but I suspect that's just a typo. The main issue is that you are trying to do what is referred to in other languages as call-by-reference. You can do this in SAS macro by passing the name of your variable rather than the value of your variable, and then use some funky&
syntax to set the variable of that name to a new value.Here is some sample code that does this:
I actually have another post on StackOverflow that has an explanation of the
&&&
syntax; you can have a look at it here. Note that the%EVAL
call has nothing to do with call-by-reference, it is just there to do the addition.