BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Hoibai
Obsidian | Level 7

why are my macro variables not resolved? Do you know it?  Thanks.

 

options mprint mlogic;

%macro test;
proc sql;
select count(*)
into :pstn_cc
from ueberschrift_lesen;
%let pstn_cc = &pstn_cc;

select pstn
into :pstn_cn1-:pstn_cn&pstn_cc
from ueberschrift_lesen;

quit;

%do i=1 %to &pstn_cc;
%put &pstn_cn&i;
%end;
%mend;
%test;

 

my log:

MLOGIC(TEST): %DO loop beginning; index variable I; start value is 1; stop value is 4; by value is 1.
MLOGIC(TEST): %PUT &pstn_cn&i
WARNING: Apparent symbolic reference PSTN_CN not resolved.
&pstn_cn1
MLOGIC(TEST): %DO loop index variable I is now 2; loop will iterate again.
MLOGIC(TEST): %PUT &pstn_cn&i
WARNING: Apparent symbolic reference PSTN_CN not resolved.
&pstn_cn2
MLOGIC(TEST): %DO loop index variable I is now 3; loop will iterate again.
MLOGIC(TEST): %PUT &pstn_cn&i
WARNING: Apparent symbolic reference PSTN_CN not resolved.
&pstn_cn3
MLOGIC(TEST): %DO loop index variable I is now 4; loop will iterate again.
MLOGIC(TEST): %PUT &pstn_cn&i
WARNING: Apparent symbolic reference PSTN_CN not resolved.
&pstn_cn4

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User
%put &pstn_cn&i;

You reference two variables here: pstn_cn (which you hever created) and i (which you created in the %DO).

To indirectly address pstn_cn1, you need to use a double ampersand:

%put &&pstn_cn&i;

This causes a double pass: in the first, && is reduced to &, and &i is resolved, so you get

&pstn_cn1

which can be resolved.

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User
%put &pstn_cn&i;

You reference two variables here: pstn_cn (which you hever created) and i (which you created in the %DO).

To indirectly address pstn_cn1, you need to use a double ampersand:

%put &&pstn_cn&i;

This causes a double pass: in the first, && is reduced to &, and &i is resolved, so you get

&pstn_cn1

which can be resolved.

Hoibai
Obsidian | Level 7

That's right. Thanks very much.

Tom
Super User Tom
Super User

Is there some reason you need transfer the values from actual data into macro variables?

If so there is no need to run the query twice just to get the count, PROC SQL will count for you.

%macro test;
%local i pstn_cc;
proc sql noprint;
select pstn
  into :pstn_cn1-
  from ueberschrift_lesen
;
%let pstn_cc = &sqlobs ;
quit;
%do i=1 %to &pstn_cc;
  %put &&pstn_cn&i;
%end;
%mend;
%test;

The problem with your code was a missing &.  You need two & so that the macro processor will know to reprocess the token after it has resolved the &I on the first pass.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 3 replies
  • 602 views
  • 2 likes
  • 3 in conversation