DATA Step, Macro, Functions and more

Macro Quoting Query

Reply
Occasional Contributor
Posts: 5

Macro Quoting Query

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!!
Super Contributor
Super Contributor
Posts: 3,174

Re: Macro Quoting Query

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

Re: Macro Quoting Query

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

   --*;
Super User
Posts: 9,681

Re: Macro Quoting Query

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
N/A
Posts: 1

Re: Macro Quoting Query

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;
Ask a Question
Discussion stats
  • 4 replies
  • 125 views
  • 0 likes
  • 5 in conversation