<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: proc sql select into with do loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-select-into-with-do-loop/m-p/970009#M376986</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/476020"&gt;@CGregg&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;In a macro I am trying to loop though a list of variables and those that are numeric columns . I want to put the name of the column in a new list of values. Example sashelp.class In: name, age, height, weight out: numlist= age, height, weight&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;In that case the code I posted should work.&amp;nbsp; If for some strange reason you actually want commas in the string just change the SEPARATED BY clause of the INTO part of the SQL query.&amp;nbsp; Perhaps you need to use the list in a SQL code instead of SAS code?&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note if you want to use the macro variable after the macro ends you will need to NOT make it LOCAL.&amp;nbsp; But in that case you should probably modify the macro to accept the name for the macro variable.&amp;nbsp; Make sure to test if the named macro variable already exists before trying to make it GLOBAL to avoid errors.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro numvars(data,varlist,mvar=numlist);
%local libname memname ;
%if not %symexist(&amp;amp;mvar) %then %global &amp;amp;mvar;
%let memname=%upcase(%scan(&amp;amp;data,-1,.));
%let libname=%upcase(%scan(work.&amp;amp;data,-2,.));

proc sql noprint;
 select name 
   into :&amp;amp;mvar separated by ' '
 from dictionary.columns
 where libname="&amp;amp;libname" and memname="&amp;amp;memname"
   and type='num'
   and findw("&amp;amp;varlist",name,',|','sitr')
 ;
quit;
%put &amp;amp;=sqlobs &amp;amp;&amp;amp;&amp;amp;mvar;
%mend numvars;

%numvars(sashelp.class,name age race);
%numvars(sashelp.cars,length|cylinders|model);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note if the dataset was created with VALIDVARNAME= option set to ANY then you probably will want to use the NLITERAL() function in the SELECT so the generated macro variable will contain name literals when the names do not conform to SAS name rules.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select nliteral(name) into ...&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 30 Jun 2025 20:13:53 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2025-06-30T20:13:53Z</dc:date>
    <item>
      <title>proc sql select into with do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-select-into-with-do-loop/m-p/970006#M376983</link>
      <description>&lt;P&gt;I am trying to loop through a list of values in the where clause and select into a var. only retaining last value.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;%macro Test (data, varlist);&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;%let dsid = %sysfunc(open(&amp;amp;data));&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;%let nobs = %sysfunc(attrn(&amp;amp;dsid,nlobs));&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;%let dsid = %sysfunc(close(&amp;amp;dsid));&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;%let nbrwords =&amp;nbsp; %sysfunc(countw(&amp;amp;varlist));&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; %put &amp;amp;nbrwords.;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;%do i = 1 %to &amp;amp;nbrwords;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; %let dsname = %scan(&amp;amp;varlist,&amp;amp;i,%str( ));;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; %let ds = %scan(&amp;amp;data, -1, '.');&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; %let ln = %scan(&amp;amp;data, +1, '.');&lt;/DIV&gt;&lt;DIV&gt;PROC SQL;&lt;/DIV&gt;&lt;DIV&gt;SELECT name into :numlist separated by ','&lt;/DIV&gt;&lt;DIV&gt;FROM dictionary.columns&lt;/DIV&gt;&lt;DIV&gt;WHERE&amp;nbsp; type eq "num" and libname eq "TESTDATA" and memname eq "TEST_DATASET1" and name eq %upcase("&amp;amp;dsname")&lt;/DIV&gt;&lt;DIV&gt;;&lt;/DIV&gt;&lt;DIV&gt;QUIT;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;%end;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&amp;nbsp;&lt;SPAN&gt; %put &amp;amp;numlist;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;%mend Test;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;%Test(testdata.test_dataset1,height weight);&lt;/SPAN&gt;&lt;/DIV&gt;</description>
      <pubDate>Mon, 30 Jun 2025 19:02:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-select-into-with-do-loop/m-p/970006#M376983</guid>
      <dc:creator>CGregg</dc:creator>
      <dc:date>2025-06-30T19:02:16Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql select into with do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-select-into-with-do-loop/m-p/970007#M376984</link>
      <description>&lt;P&gt;I am having a hard time relating the question to the code you posted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code kind of looks like it might be trying to find overlap between the set of variables in a dataset (&amp;amp;DATA) and the set of variables in a delimited string (&amp;amp;VARLIST).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So if you had DATA=SASHELP.CLASS and VARLIST= Name AGE RACE the result would be NAME AGE since they appear in SASHELP.CLASS and RACE does not.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or perhaps since you seem to only want numeric variables perhaps the result is just AGE since NAME is a character variable?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro test(data,varlist);
%local libname memname numlist ;
%let memname=%upcase(%scan(&amp;amp;data,-1,.));
%let libname=%upcase(%scan(work.&amp;amp;data,-2,.));

proc sql noprint;
 select name 
   into :numlist separated by ' '
 from dictionary.columns
 where libname="&amp;amp;libname" and memname="&amp;amp;memname"
   and type='num'
   and findw("&amp;amp;varlist",trim(name),' ','i')
 ;
quit;
%put &amp;amp;=sqlobs &amp;amp;=numlist;
%mend test;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;18   %test(sashelp.class,name age race);
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.01 seconds
      cpu time            0.00 seconds


SQLOBS=1 NUMLIST=Age
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is that what you are trying to do?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If not then please explain what you are trying to do.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Jun 2025 19:38:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-select-into-with-do-loop/m-p/970007#M376984</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-06-30T19:38:52Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql select into with do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-select-into-with-do-loop/m-p/970008#M376985</link>
      <description>In a macro I am trying to loop though a list of variables and those that are numeric columns . I want to put the name of the column in a new list of values. Example sashelp.class In: name, age, height, weight out: numlist= age, height, weight&lt;BR /&gt;</description>
      <pubDate>Mon, 30 Jun 2025 19:45:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-select-into-with-do-loop/m-p/970008#M376985</guid>
      <dc:creator>CGregg</dc:creator>
      <dc:date>2025-06-30T19:45:54Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql select into with do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-select-into-with-do-loop/m-p/970009#M376986</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/476020"&gt;@CGregg&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;In a macro I am trying to loop though a list of variables and those that are numeric columns . I want to put the name of the column in a new list of values. Example sashelp.class In: name, age, height, weight out: numlist= age, height, weight&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;In that case the code I posted should work.&amp;nbsp; If for some strange reason you actually want commas in the string just change the SEPARATED BY clause of the INTO part of the SQL query.&amp;nbsp; Perhaps you need to use the list in a SQL code instead of SAS code?&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note if you want to use the macro variable after the macro ends you will need to NOT make it LOCAL.&amp;nbsp; But in that case you should probably modify the macro to accept the name for the macro variable.&amp;nbsp; Make sure to test if the named macro variable already exists before trying to make it GLOBAL to avoid errors.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro numvars(data,varlist,mvar=numlist);
%local libname memname ;
%if not %symexist(&amp;amp;mvar) %then %global &amp;amp;mvar;
%let memname=%upcase(%scan(&amp;amp;data,-1,.));
%let libname=%upcase(%scan(work.&amp;amp;data,-2,.));

proc sql noprint;
 select name 
   into :&amp;amp;mvar separated by ' '
 from dictionary.columns
 where libname="&amp;amp;libname" and memname="&amp;amp;memname"
   and type='num'
   and findw("&amp;amp;varlist",name,',|','sitr')
 ;
quit;
%put &amp;amp;=sqlobs &amp;amp;&amp;amp;&amp;amp;mvar;
%mend numvars;

%numvars(sashelp.class,name age race);
%numvars(sashelp.cars,length|cylinders|model);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note if the dataset was created with VALIDVARNAME= option set to ANY then you probably will want to use the NLITERAL() function in the SELECT so the generated macro variable will contain name literals when the names do not conform to SAS name rules.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select nliteral(name) into ...&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Jun 2025 20:13:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-select-into-with-do-loop/m-p/970009#M376986</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-06-30T20:13:53Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql select into with do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-select-into-with-do-loop/m-p/970138#M377026</link>
      <description>Thank you for your help. I am using this macro to pass var to other macros. Is it possible to keep the same varlist order in the output(mvar)? It is currently sorting in alphabetical order.&lt;BR /&gt;</description>
      <pubDate>Wed, 02 Jul 2025 17:55:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-select-into-with-do-loop/m-p/970138#M377026</guid>
      <dc:creator>CGregg</dc:creator>
      <dc:date>2025-07-02T17:55:49Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql select into with do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-select-into-with-do-loop/m-p/970145#M377030</link>
      <description>&lt;P&gt;Which order do you want?&lt;/P&gt;
&lt;P&gt;The order they actually appear in the dataset?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro numvars(data,varlist,mvar=numlist);
%local libname memname ;
%if not %symexist(&amp;amp;mvar) %then %global &amp;amp;mvar;
%let memname=%upcase(%scan(&amp;amp;data,-1,.));
%let libname=%upcase(%scan(work.&amp;amp;data,-2,.));

proc sql noprint;
 select name 
   into :&amp;amp;mvar separated by ' '
 from dictionary.columns
 where libname="&amp;amp;libname" and memname="&amp;amp;memname"
   and type='num'
   and findw("&amp;amp;varlist",name,',|','sitr')
order by varnum
 ;
quit;
%put &amp;amp;=sqlobs &amp;amp;&amp;amp;&amp;amp;mvar;
%mend numvars;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The order they appear in the input parameter?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  order by findw("&amp;amp;varlist",name,',|','sitr')&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Something else?&lt;/P&gt;</description>
      <pubDate>Wed, 02 Jul 2025 19:08:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-select-into-with-do-loop/m-p/970145#M377030</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-07-02T19:08:52Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql select into with do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-select-into-with-do-loop/m-p/970158#M377036</link>
      <description>That did it. I never knew you could use findw in a where clause. Thanks&lt;BR /&gt;</description>
      <pubDate>Wed, 02 Jul 2025 20:27:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-select-into-with-do-loop/m-p/970158#M377036</guid>
      <dc:creator>CGregg</dc:creator>
      <dc:date>2025-07-02T20:27:49Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql select into with do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-select-into-with-do-loop/m-p/970175#M377040</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro numvars(data,varlist);
%global numlist;
%let numlist=;
proc sql noprint;
 select name into :list separated by ' '
 from dictionary.columns
 where libname="%upcase(%scan(&amp;amp;data,1,.))" and memname="%upcase(%scan(&amp;amp;data,-1,.))" and type='num';
quit; 
%do i=1 %to %sysfunc(countw(&amp;amp;varlist));
 %let temp=%scan(&amp;amp;varlist,&amp;amp;i); 
 %if %sysfunc(findw(%upcase(&amp;amp;list),%upcase(&amp;amp;temp))) %then %let numlist=&amp;amp;numlist &amp;amp;temp;
%end;
%mend numvars;

%numvars(sashelp.class,name weight height age race);

%put &amp;amp;=numlist ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;233  %macro numvars(data,varlist);
234  %global numlist;
235  %let numlist=;
236  proc sql noprint;
237   select name into :list separated by ' '
238   from dictionary.columns
239   where libname="%upcase(%scan(&amp;amp;data,1,.))" and memname="%upcase(%scan(&amp;amp;data,-1,.))"
239! and type='num';
240  quit;
241  %do i=1 %to %sysfunc(countw(&amp;amp;varlist));
242   %let temp=%scan(&amp;amp;varlist,&amp;amp;i);
243   %if %sysfunc(findw(%upcase(&amp;amp;list),%upcase(&amp;amp;temp))) %then %let numlist=&amp;amp;numlist &amp;amp;temp
243! ;
244  %end;
245  %mend numvars;
246
247  %numvars(sashelp.class,name weight height age race);
NOTE: “PROCEDURE SQL”所用时间（总处理时间）:
      实际时间          0.00 秒
      CPU 时间          0.00 秒


248
249  %put &amp;amp;=numlist ;
&lt;STRONG&gt;NUMLIST=weight height age
&lt;/STRONG&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 03 Jul 2025 02:02:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-select-into-with-do-loop/m-p/970175#M377040</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2025-07-03T02:02:11Z</dc:date>
    </item>
  </channel>
</rss>

