BookmarkSubscribeRSS Feed
wbguy
Fluorite | Level 6
Hi All,

I am trying to run the following, iteratively:


proc sql noprint;
select name
into :vlist2 separated by ' '
from varnames1 where type = 1 and iter =0;
quit;

In iteration i,the input file is varnamesi and the output is the macro variable &vlist(i+1), i=1,2,...,10.

However, the following code does not work:

%macro stepwise;
some statements here.....
...............
proc sql noprint;
select name
into %str(:vlist)%eval(&i.+1) separated by %str(' ')
from varnames&i. where type = 1 and iter=0;
quit;
%mend;

How can I fix this? Is the first %str() function needed at all? Thanks a lot!!
4 REPLIES 4
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Honestly, your macro code is incomplete - where is the %DO / %END where you iterate through &I values.

Suggest turning on options below so you can see the code being generated in the SAS log (consider activating MLOGIC as needed):

OPTIONS SOURCE SOURCE2 MACROGEN SYMBOLGEN /* MLOGIC */ MPRINT;

If still a problem after additional desk-checking, come back to the forum with a post-reply and COPY/PASTE the exact SAS log output, rather than your selective code snippet.

Scott Barry
SBBWorks, Inc.
chang_y_chung_hotmail_com
Obsidian | Level 7
Here is one way. I find it easier to code and to test if the looping mechanism is separated out.



   /* test data */

   data class;

     set sashelp.class;

     id = _n_;

   run;

 

   %macro doOne(i);

     proc sql noprint;

       select name into :vlist&i separated by " "

       from class

       where id <= &i;

     quit;

   %mend  doOne;

 

   %macro dofive;

     %local i;

     %do i = 1 %to 5;

       %local vlist&i;

       %doOne(&i)

       %put vlist&i=***&&vlist&i***;

     %end;

   %mend  dofive;

 

   %dofive

   %*-- on log

   vlist1=***Alfred***

   vlist2=***Alfred Alice***

   vlist3=***Alfred Alice Barbara***

   vlist4=***Alfred Alice Barbara Carol***

   vlist5=***Alfred Alice Barbara Carol Henry***

   --*;
Ksharp
Super User
It looks like you need code like this
[pre]
%macro stepwise;
some statements here.....
...............

proc sql noprint;
%do i=1 %to 10;
%let j=%eval(&i+1);
select name
into : vlist&j separated by ' '
from varnames&i. where type = 1 and iter=0;
%end;

quit;
%mend;
[/pre]

Ksharp
gch
Calcite | Level 5 gch
Calcite | Level 5
You don't need %str() function in your code. Try this one:

%macro stepwise;
%do i=1 %to 10;
%global vlist&i;
proc sql noprint;
select name
into :vlist&i separated by ' '
from varnames&i;
quit;
%end;
%mend;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 4 replies
  • 1406 views
  • 0 likes
  • 5 in conversation