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;

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