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

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

2 REPLIES 2
Astounding
PROC Star

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 2 replies
  • 9843 views
  • 4 likes
  • 3 in conversation