BookmarkSubscribeRSS Feed
newbie
Calcite | Level 5

In th below code as you can see im trying to create a macro variable for each iteration of 'i' but when i try to print them onto saslog im getting the error

as Apparent symbolic reference ST1not resolved .

Any help would be appreciated!

%macro do(m,n);

%let i=;

data _null_;

%do i=&m %to &n;

  st=%eval(&i+1);

  call symput("st&i",st);

   %put &&st&i;

%end;

run;

%mend;

%do(1,2)

2 REPLIES 2
art297
Opal | Level 21

I can't test your code at the moment, but I doubt if it will work for a couple of reasons: (1) I think %do is a reserved word and (2) the macro variable won't resolve within the same datastep it is created.  Try something like the following:

%macro doit(m,n);

  %do i=&m %to &n;

    data _null_;

      st=%eval(&i+1);

      call symput("st&i",st);

    run;

    %put &&st&i;

  %end;

%mend;

%doit(1,2)

Tom
Super User Tom
Super User

You are mixing up macro logic and data step logic. The reason that the %PUT is getting an error is that it is compiled BEFORE the data step runs.  So the CALL SYMPUT function has not been called yet.

In a macro use the %LET statement to assign a value to a macro variable.

%macro doit(m,n);

  %do i=&m %to &n;

    %let st&i=%eval(&i+1);

  %end;

%mend doit;

%doit(1,2);

In a data step use the DO statement to loop.

%let m=1;

%let n=2;

data _null_;

  do i=&m to &n;

    call symputx(cats('ST',i),i+1);

  end;

run;

PS: Do not use DO as the name of a macro as %DO is already a macro statement.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 677 views
  • 6 likes
  • 3 in conversation