Okay. This is getting on my nerves. Just when I think I know how to access macro variables with multiple ampersands, something goes wrong. Can someone correct my code so I can display the values 7 and 624? I have tried all kinds of combinations of ampersands and periods, but the closest I can get it to display "BRON_BRON_R1_TERM_CNT" and "CM_TERM_CNT" instead of "7" and "624".
%let dsname1=BRON_BRON_R1;
%let dsname2=CM;
%let BRON_BRON_R1_TERM_CNT=7;
%let CM_TERM_CTN=624;
%macro loop;
%do i=1 %to 2;
%put &&&&dsname&i.._TERM_CNT;
%end;
%mend loop;
%loop;
Sorry, I read the problem wrong. Is it that your original code is correct, except for switching the "TN" vs. "NT" in the %LET statement?
********************
EDITED:
Besides that, I think you need to use 6 ampersands.
If you cut down the number of & from 4 down to 2, you should be fine.
That doesn't work for me. I get the values "BRON_BRON_R1_TERM_CNT" and "CM_TERM_CNT" instead of "7" and "624".
Using && converts &&dsname&i. to &dsname1, which converts to BRON_BRON_R1, so I am left with BRON_BRON_R1_TERM_CNT. I now need SAS to recognize &BRON_BRON_R1_TERM_CNT, which is why I added the extra && in front.
If i use &&dsname&i.._TERM_CNT, I get:
MLOGIC(LOOP): Beginning execution.
MLOGIC(LOOP): %DO loop beginning; index variable I; start value is 1; stop value is 2; by value is
1.
MLOGIC(LOOP): %PUT &&dsname&i.._TERM_CNT
SYMBOLGEN: && resolves to &.
SYMBOLGEN: Macro variable I resolves to 1
SYMBOLGEN: Macro variable DSNAME1 resolves to BRON_BRON_R1
BRON_BRON_R1_TERM_CNT
MLOGIC(LOOP): %DO loop index variable I is now 2; loop will iterate again.
MLOGIC(LOOP): %PUT &&dsname&i.._TERM_CNT
SYMBOLGEN: && resolves to &.
SYMBOLGEN: Macro variable I resolves to 2
SYMBOLGEN: Macro variable DSNAME2 resolves to CM
CM_TERM_CNT
MLOGIC(LOOP): %DO loop index variable I is now 3; loop will not iterate again.
MLOGIC(LOOP): Ending execution.
If I use &&&&dsname&i.._TERM_CNT, I get the exact same thing:
MLOGIC(LOOP): Beginning execution.
MLOGIC(LOOP): %DO loop beginning; index variable I; start value is 1; stop value is 2; by value is
1.
MLOGIC(LOOP): %PUT &&&&dsname&i.._TERM_CNT
SYMBOLGEN: && resolves to &.
SYMBOLGEN: && resolves to &.
SYMBOLGEN: Macro variable I resolves to 1
SYMBOLGEN: && resolves to &.
SYMBOLGEN: Macro variable DSNAME1 resolves to BRON_BRON_R1
BRON_BRON_R1_TERM_CNT
MLOGIC(LOOP): %DO loop index variable I is now 2; loop will iterate again.
MLOGIC(LOOP): %PUT &&&&dsname&i.._TERM_CNT
SYMBOLGEN: && resolves to &.
SYMBOLGEN: && resolves to &.
SYMBOLGEN: Macro variable I resolves to 2
SYMBOLGEN: && resolves to &.
SYMBOLGEN: Macro variable DSNAME2 resolves to CM
CM_TERM_CNT
MLOGIC(LOOP): %DO loop index variable I is now 3; loop will not iterate again.
MLOGIC(LOOP): Ending execution.
BUT WHY!!!
Sorry, I read the problem wrong. Is it that your original code is correct, except for switching the "TN" vs. "NT" in the %LET statement?
********************
EDITED:
Besides that, I think you need to use 6 ampersands.
Oh, good catch on the misspelling. But that obviously wasn't the problem since BRON_BRON_R1_TERM_CNT wasn't misspelled.
Still, I tried the 6 ampersands, and that seems to have done it. I can never tell how many to use. I've never needed more than 4, so I never went beyond that.
Thank you so much!
You could make your life a whole lot simpler if you just didn't have to have _TERM_CNT at the end of the macro variable names. You could even put TERM_CNT_ at the beginning to achieve this simplicity.
My breaking point was 5. I basically changed my approach to macros that night.
When macros get really complex, consider spooling the problem out and then including it back in.
data _null_ ;
file myFile;
do i = 1 to 3 ;
put 'test_' i ;
end;
run;
%include myFile;
Simplifies debugging TREMENDOUSLY. There are other ways than that too: just get creative...but readable.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.