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

Hi all,

I am trying to take the result of a macro variable which itself is the name of a macro variable and get that to resolve to its value.

The macro

 

/*Macro variable SegmentA will already have the value 100 */

%let SegmentA= 100;

 

/*I pull the Segment values I need from a dataset and use the SQL INTO to get a macro variable with the value of the segment*/

%let seg1= SegmentA;

 

%put &&seg1;/*this doesn't work*/

/*How do I get the value of seg1 (SegmentA) to resolve to its macro value (100)?*/

 

/*If you know how to get Seg1 to return 100, you don't need to look further below*/

 

 

/*Actual Code*/

/*Here is more of the code so you can maybe see why I am trying to do this*/

/*The macros with the 'numeric' values have already been established upstream*/

/*The point of this is that I have a specific # of records I need to pull from each segmentation.

That number of records per segmentation is stored in a macro variable of the same name as the segmentation*/

 

%macro Test;

%local i;

Proc SQL;

  select distinct segmentation

  from p.seg

  where segmentation ne ' '

  order by segmentation;

 

  select distinct segmentation into :s1 - :s&sqlobs.

  from p.seg

  where segmentation ne ' '

  order by segmentation;

quit;

 

%do i=1 %to &sqlobs;

  %put run# &i. of &sqlobs. for macro_container=&&s&i.;

  %let mc= &&s&i;

 

/*Set ts=1 if the record is under the threshold for that segmentation*/

data temp;

set p.seg;

 if _n_ <= &mc. then ts=1; /*Also need to determine how to get this to resolve as an integer. Input(&mc.,8.) didn't work*/

 where segmentation=%unquote(%nrquote(%'&mc.%')); /*Limit to just 1 segmentation at a time*/

run;

 

/*Update the segmentation to segmentationvalue_overlimit if ts=0 from Temp because those are the records in the segmentation over the # limit I need*/

proc sql;

  update p.seg as a

  set segmentation=( select cats(segmentation,'_overlimit') from temp b where   a.acc_id=b.acc_id and ts=0 )

;quit;

 

 

%end;

%mend;

 

%Test;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
heffo
Pyrite | Level 9

To expand what @WarrenKuhfeld said.

When SAS sees an & the compiler starts looking for macro variables. But, if it sees two of them, it replaces them with one, then continues to look for more macro variables. When it is done with the first loop of the checking, it does it again to see if there are any "new" references. 

So, in your case:

%put &&&seg1;

SAS now translates && -> & and &seg1 -> SegmentA.

 

Now we have:

%put &SegmentA;

 

SAS now does the last translation &SegmentA -> 100. 

Now we have:

%put 100;

As we don't have any more macro variables, SAS looks at macro programs (anything with %). In this case %put. It runs that code and prints "100" to the log file. 

 

View solution in original post

2 REPLIES 2
heffo
Pyrite | Level 9

To expand what @WarrenKuhfeld said.

When SAS sees an & the compiler starts looking for macro variables. But, if it sees two of them, it replaces them with one, then continues to look for more macro variables. When it is done with the first loop of the checking, it does it again to see if there are any "new" references. 

So, in your case:

%put &&&seg1;

SAS now translates && -> & and &seg1 -> SegmentA.

 

Now we have:

%put &SegmentA;

 

SAS now does the last translation &SegmentA -> 100. 

Now we have:

%put 100;

As we don't have any more macro variables, SAS looks at macro programs (anything with %). In this case %put. It runs that code and prints "100" to the log file. 

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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