<?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: Loop in SQL into: in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Loop-in-SQL-into/m-p/729683#M227130</link>
    <description>&lt;P&gt;There is no need to use any macro code to do that.&amp;nbsp; You can just do it in a data step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set have;
  call symputx(cats('var_',_n_),var);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or even in an SQL step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select var into :var_1 - from have;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But WHY are you taking data out of your dataset and putting it into macro variables?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why not just do what ever operations you are doing on all of the observations in HAVE at the same time?&lt;/P&gt;
&lt;P&gt;And if do need to use the value of VAR to run some complex macro then just call the macro right from the data step and skip defining all of those macro varaibles.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set have;
  call execute(cats('%nrstr(%mymacro)(',var,')'));
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 29 Mar 2021 02:20:50 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2021-03-29T02:20:50Z</dc:date>
    <item>
      <title>Loop in SQL into:</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-in-SQL-into/m-p/729680#M227128</link>
      <description>&lt;P&gt;Hi Everyone,&lt;/P&gt;
&lt;P&gt;Can you please help me with the below issue?&lt;BR /&gt;For each row, I want to create a macro variable taking value of&amp;nbsp; "var" column as below.&lt;BR /&gt;I can do it row by row till the end, however is there anyway to create a macro do something like &lt;BR /&gt;%DO i=1 %to endoffile?&lt;/P&gt;
&lt;P&gt;Thank you,&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id var;
datalines;
1 111
2 222
3 333
;
run;

%macro mymacro(c=);
	data _temp; set have;
	if _N_=&amp;amp;c;
	run;

	proc sql;
	select var into: var_&amp;amp;c from _temp;
	quit;
%mend;
%mymacro(c=1);
%mymacro(c=2);
%mymacro(c=3);

%put &amp;amp;var_1; *should returnn value 111;
%put &amp;amp;var_2; *should returnn value 222;
%put &amp;amp;var_3; *should returnn value 333;&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, 29 Mar 2021 02:10:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-in-SQL-into/m-p/729680#M227128</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2021-03-29T02:10:34Z</dc:date>
    </item>
    <item>
      <title>Re: Loop in SQL into:</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-in-SQL-into/m-p/729683#M227130</link>
      <description>&lt;P&gt;There is no need to use any macro code to do that.&amp;nbsp; You can just do it in a data step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set have;
  call symputx(cats('var_',_n_),var);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or even in an SQL step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select var into :var_1 - from have;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But WHY are you taking data out of your dataset and putting it into macro variables?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why not just do what ever operations you are doing on all of the observations in HAVE at the same time?&lt;/P&gt;
&lt;P&gt;And if do need to use the value of VAR to run some complex macro then just call the macro right from the data step and skip defining all of those macro varaibles.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set have;
  call execute(cats('%nrstr(%mymacro)(',var,')'));
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 29 Mar 2021 02:20:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-in-SQL-into/m-p/729683#M227130</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-03-29T02:20:50Z</dc:date>
    </item>
    <item>
      <title>Re: Loop in SQL into:</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-in-SQL-into/m-p/729687#M227131</link>
      <description>&lt;P&gt;Hi Tom,&lt;/P&gt;
&lt;P&gt;My real work is that I have 2 columns:&lt;/P&gt;
&lt;P&gt;file_name | List_of_Some_element&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For each rows, I want to create 2 variables:&amp;nbsp; &amp;amp;filename , and &amp;amp;list_of_some_element&lt;/P&gt;
&lt;P&gt;Then I will do the SQL like&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;create table Mydata as as select a.*,&amp;nbsp;&amp;amp;list_of_some_element &lt;BR /&gt;from Mydata join&amp;nbsp;&amp;amp;filename
on a.xyz=b.xyz; quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That's why I want to store the column value into macro variable.&lt;/P&gt;
&lt;P&gt;So after looping through all rows, the final MyData will have all elements I need to use.&lt;/P&gt;
&lt;P&gt;Thank you,&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 Mar 2021 03:53:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-in-SQL-into/m-p/729687#M227131</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2021-03-29T03:53:57Z</dc:date>
    </item>
    <item>
      <title>Re: Loop in SQL into:</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-in-SQL-into/m-p/729688#M227132</link>
      <description>&lt;P&gt;If I understand correctly what you want, then you can&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  select var into :var_1- from have order by id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then you can loop VAR_1 through VAR_&amp;amp;sqlobs for your subsequent work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRIKE&gt;Just be sure to use an upper limit (200 in my example) that is at least as large as the number of observations in HAVE.&amp;nbsp;&lt;/STRIKE&gt;&amp;nbsp; Just remembered you don't need a specific upper limit.&lt;BR /&gt;&lt;BR /&gt;True, if you have ID's 1,2,3 and 6, but not 4 and 5, then macrovar VAR_4 will have value 666.&amp;nbsp; But that does not appear to be a problem for the task you describe.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 Mar 2021 04:02:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-in-SQL-into/m-p/729688#M227132</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-03-29T04:02:04Z</dc:date>
    </item>
    <item>
      <title>Re: Loop in SQL into:</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-in-SQL-into/m-p/729706#M227144</link>
      <description>&lt;P&gt;Like this?&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;data _null_;
  set HAVE;
  call execute(catx(' ', 'proc sql; create table MYDATA'|| put(_N_,9.0 -l), 'as'
                       , 'select a.*,', LIST_OF_SOME_ELEMENTS
                       , 'from MYDATA join', FILENAME
                       , 'on a.xyz=b.xyz; quit;'
              ));
run;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 Mar 2021 05:29:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-in-SQL-into/m-p/729706#M227144</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-03-29T05:29:13Z</dc:date>
    </item>
    <item>
      <title>Re: Loop in SQL into:</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-in-SQL-into/m-p/729721#M227155</link>
      <description>&lt;P&gt;Use CALL EXECUTE straight from the dataset.&lt;/P&gt;</description>
      <pubDate>Mon, 29 Mar 2021 08:29:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-in-SQL-into/m-p/729721#M227155</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-03-29T08:29:16Z</dc:date>
    </item>
    <item>
      <title>Re: Loop in SQL into:</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-in-SQL-into/m-p/729757#M227168</link>
      <description>&lt;P&gt;Sounds like the goal is to use the list of variables to merge bunch of datasets.&amp;nbsp; So assuming that the datasets with the "variables" have a maximum of one observation per value of XYZ then should be able to just use one data step to combine all of them.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  merge mydata 
        dataset1(keep=xyz vara )
        dataset2(keep=xyz varb varc)
        dataset3(keep=xyz vard)
  ;
  by xyz;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So just use the data in your metadata table to generate that code.&amp;nbsp; So if your metadata set looks like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data varlist ;
   input dataset :$41. varname :$32. ;
cards;
dataset1 vara
dataset2 varb
dataset2 varc
dataset3 vard
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then this data step will generate the code and the %INCLUDE will run it.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename code temp;
data _null_;
  set varlist end=eof;
  by dataset ;
  file code;
  if _n_=1 then put 'data want;' / @3 'merge mydata ';
  if first.dataset then put @9 dataset '(keep=xyz ' @;
  put varname @;
  if last.dataset then put ')';
  if eof then put @3 ';' / @3 'by xyz;' / 'run;' ;
run;
%include code / source2;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 29 Mar 2021 11:51:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-in-SQL-into/m-p/729757#M227168</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-03-29T11:51:22Z</dc:date>
    </item>
  </channel>
</rss>

