Desktop productivity for business analysts and programmers

Retrieve value from a macro variable to another macro variable uing a macro/loop

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 80
Accepted Solution

Retrieve value from a macro variable to another macro variable uing a macro/loop

There are a bunch of global macro variables with assigned value.

 

I would like to use a loop to do some stuff in a macro. At the beginning of the macro, will need to define a new macro variable, and retrieve value from those global macro variables.

 

My code is:

 

%let Period_1 = 11;
%let Period_2 = 22;
%let Period_3 = 33;
%global Period_temp;

%macro test;

%do _j=1 %to 3;

%let Period_temp = &&&(Period_&_j);           /* something might be wrong here */
...

	%end;
%mend test;
%test;

%PUT _ALL_;
run;

/* The log shows:

GLOBAL PERIOD_1 11
GLOBAL PERIOD_2 22
GLOBAL PERIOD_3 33
GLOBAL PERIOD_TEMP &(Period_3)          /* which is not 33 */

*/

Clearly Period_temp is not picking up 'the numeric value', i.e. 11, 22, or 33.

 

How should I change the code?

---------------------------------------------------------------------------------------------------------------------------------

What if I want Period_temp to be some calculation result involving Period_1, Period_2, Period_3?

e.g. Period_temp = 11 + 22 * 33; How should I change the code?


Accepted Solutions
Solution
‎03-01-2017 06:40 PM
Super User
Posts: 5,353

Re: Retrieve value from a macro variable to another macro variable uing a macro/loop

To retrieve a global variable:

 

%let period_temp = &&period_&j;

 

The calculation you want isn't clear.  Did you really intend to apply both addition and multiplication?

 

Assuming you want &PERIOD_TEMP to be 11, then 33, then 66 (the cumulative value of all the global variables so far), you could code:

 

%global period_temp;

%let period_temp=0;

 

Then within the macro:

 

%let period_temp = %eval(&period_temp + &&period_&j);

 

If you actually mean to use the formula you posted, you could simply code:

 

%let period_temp = %eval(&period_1 + &period_2 * &period_3);

 

The %EVAL function is what forces macro language to perform the calculations.

View solution in original post


All Replies
Solution
‎03-01-2017 06:40 PM
Super User
Posts: 5,353

Re: Retrieve value from a macro variable to another macro variable uing a macro/loop

To retrieve a global variable:

 

%let period_temp = &&period_&j;

 

The calculation you want isn't clear.  Did you really intend to apply both addition and multiplication?

 

Assuming you want &PERIOD_TEMP to be 11, then 33, then 66 (the cumulative value of all the global variables so far), you could code:

 

%global period_temp;

%let period_temp=0;

 

Then within the macro:

 

%let period_temp = %eval(&period_temp + &&period_&j);

 

If you actually mean to use the formula you posted, you could simply code:

 

%let period_temp = %eval(&period_1 + &period_2 * &period_3);

 

The %EVAL function is what forces macro language to perform the calculations.

Super User
Super User
Posts: 7,682

Re: Retrieve value from a macro variable to another macro variable uing a macro/loop

Why are you putting numeric "data" into text based macros?  It would benefit you far greater to put your period timepoint "data" in "datasets" - clue is in the name.  Then you can use merging (joining), lookups, datasteps and such like to work on the data.  Macro is a find/replace system, which modifies the actual text code sent to the compiler, it is not a data processing utility.  For instance, in a datastep you could do:

data want;
  do i=11, 22, 33;
    put _all_;
  end;
run;

 Much simpler, easier to understand code.

☑ This topic is solved.

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

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