BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
djbateman
Lapis Lazuli | Level 10

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;
1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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. 

View solution in original post

6 REPLIES 6
Astounding
PROC Star

If you cut down the number of & from 4 down to 2, you should be fine.

djbateman
Lapis Lazuli | Level 10

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!!!

 

Astounding
PROC Star

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. 

djbateman
Lapis Lazuli | Level 10

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!

PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
AlanC
Barite | Level 11

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.

https://github.com/savian-net

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

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
  • 6 replies
  • 2843 views
  • 1 like
  • 4 in conversation