<?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: iterating loop in sql in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/iterating-loop-in-sql/m-p/537349#M147783</link>
    <description>&lt;P&gt;Two problems with your code:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;You used &amp;amp;i instead of &amp;amp;s in the %SCAN call&lt;/LI&gt;
&lt;LI&gt;The first macro parameter is unnecessary&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;So, I think that hat you want to do is something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select distinct sex into:gen separated by "~" from sashelp.class;
quit;
options symbolgen mprint;
%put &amp;amp;gen;
%macro prnt(i,upto);
  %local s var;
  %do s=&amp;amp;i %to &amp;amp;upto;
  %let var=%scan(&amp;amp;gen,&amp;amp;s,'~');
  Proc sql;
    create table newt_&amp;amp;var as
    select * from sashelp.class
    where sex="&amp;amp;var";
  quit;
  %end;
%mend;
%prnt (1,2);

&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 21 Feb 2019 09:28:01 GMT</pubDate>
    <dc:creator>s_lassen</dc:creator>
    <dc:date>2019-02-21T09:28:01Z</dc:date>
    <item>
      <title>iterating loop in sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/iterating-loop-in-sql/m-p/537347#M147781</link>
      <description>&lt;P&gt;Hi Experts,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i am trying to iterate the below mentioned code. Code is running without any error but its not iterating. Tried changing the do statement before and after the Proc sql but not working.Could anyone please help out how to iterate the loop in sql.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your help in advance.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select distinct sex into:gen separated by "~" from sashelp.class;
quit;

options symbolgen mprint;
%put &amp;amp;gen;

%macro prnt(var,i,upto);
%do s=&amp;amp;i %to &amp;amp;upto;
%let var=%scan(&amp;amp;gen,&amp;amp;i,'~');
Proc sql;
create table newt_&amp;amp;var as
select * from sashelp.class
where sex="&amp;amp;var";
quit;
%end;
%mend;


%prnt (&amp;amp;var,1,2);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 21 Feb 2019 09:17:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/iterating-loop-in-sql/m-p/537347#M147781</guid>
      <dc:creator>sasismylife</dc:creator>
      <dc:date>2019-02-21T09:17:58Z</dc:date>
    </item>
    <item>
      <title>Re: iterating loop in sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/iterating-loop-in-sql/m-p/537349#M147783</link>
      <description>&lt;P&gt;Two problems with your code:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;You used &amp;amp;i instead of &amp;amp;s in the %SCAN call&lt;/LI&gt;
&lt;LI&gt;The first macro parameter is unnecessary&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;So, I think that hat you want to do is something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select distinct sex into:gen separated by "~" from sashelp.class;
quit;
options symbolgen mprint;
%put &amp;amp;gen;
%macro prnt(i,upto);
  %local s var;
  %do s=&amp;amp;i %to &amp;amp;upto;
  %let var=%scan(&amp;amp;gen,&amp;amp;s,'~');
  Proc sql;
    create table newt_&amp;amp;var as
    select * from sashelp.class
    where sex="&amp;amp;var";
  quit;
  %end;
%mend;
%prnt (1,2);

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 21 Feb 2019 09:28:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/iterating-loop-in-sql/m-p/537349#M147783</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2019-02-21T09:28:01Z</dc:date>
    </item>
    <item>
      <title>Re: iterating loop in sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/iterating-loop-in-sql/m-p/537351#M147784</link>
      <description>&lt;P&gt;Thank you Lassen for response. it helps me a lot &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Feb 2019 09:39:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/iterating-loop-in-sql/m-p/537351#M147784</guid>
      <dc:creator>sasismylife</dc:creator>
      <dc:date>2019-02-21T09:39:34Z</dc:date>
    </item>
    <item>
      <title>Re: iterating loop in sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/iterating-loop-in-sql/m-p/537354#M147785</link>
      <description>&lt;P&gt;And what is it your actually trying to achieve?&amp;nbsp; Its almost never a good idea to split like data up, it incurs more read/write blocks, more header blocks on the file so more storage needs, creates a need for messy hard to maintain code etc.&amp;nbsp; There is a functionality in Base SAS specifically for this type of grouped runs, it is called by group processing, and it is both faster and easier to code than trying to reproduce this yourself.&amp;nbsp; So take your example, and add to it that your doing this to print m/f separately, you can simply do:&lt;/P&gt;
&lt;PRE&gt;proc report data=sashelp.class;
  columns _all_;
  by sex;
  title "#byval1";
run;
&lt;/PRE&gt;
&lt;P&gt;The key part here is the by sex; which will automatically group the data into distinct groups based on the variables provided, and block out results for those distinct groups.&amp;nbsp; The title just includes a means to show the distinct group in question in a title.&amp;nbsp; This functionality is available to all procedures/datastep etc.&lt;/P&gt;
&lt;P&gt;Don't try to re-invent the wheel.&lt;/P&gt;</description>
      <pubDate>Thu, 21 Feb 2019 09:45:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/iterating-loop-in-sql/m-p/537354#M147785</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2019-02-21T09:45:51Z</dc:date>
    </item>
  </channel>
</rss>

