DATA Step, Macro, Functions and more

Macro Variables with Multiple Ampersands

Accepted Solution Solved
Reply
Regular Contributor
Posts: 247
Accepted Solution

Macro Variables with Multiple Ampersands

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;

Accepted Solutions
Solution
‎11-01-2017 02:55 PM
Super User
Posts: 6,934

Re: Macro Variables with Multiple Ampersands

[ Edited ]
Posted in reply to djbateman

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


All Replies
Super User
Posts: 6,934

Re: Macro Variables with Multiple Ampersands

Posted in reply to djbateman

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

Regular Contributor
Posts: 247

Re: Macro Variables with Multiple Ampersands

Posted in reply to Astounding

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

 

Solution
‎11-01-2017 02:55 PM
Super User
Posts: 6,934

Re: Macro Variables with Multiple Ampersands

[ Edited ]
Posted in reply to djbateman

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. 

Regular Contributor
Posts: 247

Re: Macro Variables with Multiple Ampersands

Posted in reply to Astounding

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!

Respected Advisor
Posts: 3,278

Re: Macro Variables with Multiple Ampersands

Posted in reply to djbateman

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
Regular Contributor
Posts: 150

Re: Macro Variables with Multiple Ampersands

Posted in reply to djbateman

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.

☑ This topic is solved.

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

Discussion stats
  • 6 replies
  • 292 views
  • 1 like
  • 4 in conversation