<?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: Newbie Question - How to do a loop in SAS combine with proc sql? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Newbie-Question-How-to-do-a-loop-in-SAS-combine-with-proc-sql/m-p/508629#M136627</link>
    <description>&lt;P&gt;There are many ways to solve this type of problem, one way is to generate code and then execute the generated code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Generate code */
filename pgm temp;
data _null_;
  file pgm;
  length class summary current $32;
  do i = 1 to 2;
    if i = 1 then class = "bank";
    else if i = 2 then class = "bonds";
    summary = catt(class,'_summary');
    current = catt(class,'_curr');
    put 'proc sql noprint;'
      / '  create table ' summary 'as'
      / '    select Market_Value'
      / '    from ' current ';'
      / 'quit;'
      ;
end;
run;
/* Excute the generated code */
%inc pgm  / source2;
filename pgm clear;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then code get written to a temporary file in the data step and then included using the %inc statement.&lt;/P&gt;
&lt;P&gt;The / Source2 option shows the included code in the log.&lt;/P&gt;</description>
    <pubDate>Tue, 30 Oct 2018 10:25:15 GMT</pubDate>
    <dc:creator>MichaelLarsen</dc:creator>
    <dc:date>2018-10-30T10:25:15Z</dc:date>
    <item>
      <title>Newbie Question - How to do a loop in SAS combine with proc sql?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Newbie-Question-How-to-do-a-loop-in-SAS-combine-with-proc-sql/m-p/508602#M136614</link>
      <description>&lt;P&gt;Hi I want to create a loop for the following code so I can output a range of names...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="2"&gt;_null_&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;do&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; i = &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="Courier New" size="2"&gt;to&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;2&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;if&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; i = &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="Courier New" size="2"&gt;then&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; class = &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="2"&gt;"bank"&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;if&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; i = &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;2&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="Courier New" size="2"&gt;then&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; class = &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="2"&gt;"bonds"&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;end&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;summary = cats(class,&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="2"&gt;"_summary"&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;call&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; symputx(&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="2"&gt;'summary'&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;,summary);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;Current = cats(class,&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="2"&gt;"_curr"&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;call&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; symputx(&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="2"&gt;'current'&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;,current);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;%put&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; &amp;amp;summary.;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;%put&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; &amp;amp;current.;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;But right now my output looks like this &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;bond_summary&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;STRONG&gt;bond_curr&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;What I dont understand is why &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;1) my first i =1 clas = bank didnt appear?&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;2) why is an "s" missing in my "bond"&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;And then if the above code is run successfully I want to incorporate the following proc sql code&amp;nbsp;but I dont know where to stick the code in the loop. &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt;&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;sql&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;create&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="2"&gt;table&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; &amp;amp;&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;summary.&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="2"&gt;as&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;select&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; Market_value&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;from&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; &amp;amp;&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;current.&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;STRONG&gt;quit&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;Many thanks for helping!&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Oct 2018 08:48:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Newbie-Question-How-to-do-a-loop-in-SAS-combine-with-proc-sql/m-p/508602#M136614</guid>
      <dc:creator>irisG</dc:creator>
      <dc:date>2018-10-30T08:48:36Z</dc:date>
    </item>
    <item>
      <title>Re: Newbie Question - How to do a loop in SAS combine with proc sql?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Newbie-Question-How-to-do-a-loop-in-SAS-combine-with-proc-sql/m-p/508625#M136624</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1. To see i can class variable&amp;nbsp;output, you have to add put statement.&amp;nbsp;&lt;/P&gt;&lt;P&gt;2. As per loop , when i=1 then SAS automatically assign length as per&amp;nbsp;first declaration . To avoid truncation you have to use LENGTH&amp;nbsp;statement .&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
length class $5.;
	do i=1 to 2;

		if i=1 then
			class="bank";

		if i=2 then
			class="bonds";
          put i= class=;
	end;
	summary=cats(class, "_summary");
	call symputx('summary', summary);
	Current=cats(class, "_curr");
	call symputx('current', current);
run;

%put &amp;amp;summary.;
%put &amp;amp;current.;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 30 Oct 2018 10:02:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Newbie-Question-How-to-do-a-loop-in-SAS-combine-with-proc-sql/m-p/508625#M136624</guid>
      <dc:creator>singhsahab</dc:creator>
      <dc:date>2018-10-30T10:02:03Z</dc:date>
    </item>
    <item>
      <title>Re: Newbie Question - How to do a loop in SAS combine with proc sql?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Newbie-Question-How-to-do-a-loop-in-SAS-combine-with-proc-sql/m-p/508629#M136627</link>
      <description>&lt;P&gt;There are many ways to solve this type of problem, one way is to generate code and then execute the generated code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Generate code */
filename pgm temp;
data _null_;
  file pgm;
  length class summary current $32;
  do i = 1 to 2;
    if i = 1 then class = "bank";
    else if i = 2 then class = "bonds";
    summary = catt(class,'_summary');
    current = catt(class,'_curr');
    put 'proc sql noprint;'
      / '  create table ' summary 'as'
      / '    select Market_Value'
      / '    from ' current ';'
      / 'quit;'
      ;
end;
run;
/* Excute the generated code */
%inc pgm  / source2;
filename pgm clear;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then code get written to a temporary file in the data step and then included using the %inc statement.&lt;/P&gt;
&lt;P&gt;The / Source2 option shows the included code in the log.&lt;/P&gt;</description>
      <pubDate>Tue, 30 Oct 2018 10:25:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Newbie-Question-How-to-do-a-loop-in-SAS-combine-with-proc-sql/m-p/508629#M136627</guid>
      <dc:creator>MichaelLarsen</dc:creator>
      <dc:date>2018-10-30T10:25:15Z</dc:date>
    </item>
    <item>
      <title>Re: Newbie Question - How to do a loop in SAS combine with proc sql?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Newbie-Question-How-to-do-a-loop-in-SAS-combine-with-proc-sql/m-p/508645#M136635</link>
      <description>&lt;P&gt;Great! Works like a charm! Many thanks!&lt;/P&gt;</description>
      <pubDate>Tue, 30 Oct 2018 11:43:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Newbie-Question-How-to-do-a-loop-in-SAS-combine-with-proc-sql/m-p/508645#M136635</guid>
      <dc:creator>irisG</dc:creator>
      <dc:date>2018-10-30T11:43:23Z</dc:date>
    </item>
    <item>
      <title>Re: Newbie Question - How to do a loop in SAS combine with proc sql?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Newbie-Question-How-to-do-a-loop-in-SAS-combine-with-proc-sql/m-p/508649#M136637</link>
      <description>If it works like a charm, you should mark it as the solution!</description>
      <pubDate>Tue, 30 Oct 2018 12:21:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Newbie-Question-How-to-do-a-loop-in-SAS-combine-with-proc-sql/m-p/508649#M136637</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2018-10-30T12:21:32Z</dc:date>
    </item>
  </channel>
</rss>

