<?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 proc sql  / do loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-do-loop/m-p/516210#M139387</link>
    <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a set of numbers that I need to collect the count for. Essentially, I need to perform :&amp;nbsp;&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;proc sql;
create table lala&amp;amp;i as
select count(*)
from singlefamilyhht
where persons=4 and hinc&amp;lt;.8*(i)
or
persons=3 and hinc&amp;lt; .9*(.8*(i))
or
 persons=2 and hinc&amp;lt; .8*(.8*(i))
or
 persons=5 and hinc&amp;lt; 1.08*(.8*(i))
or
persons=6 and hinc&amp;lt; 1.16*(.8*(i))
group by puma1;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;repeatedly for different medians (@i).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I wrote the following code:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro data;
%let median = {61000.00 68000.00 68400.00 88000.00 77400.00 84040.00 103000.00 65400.00 47800.00 75000.00 92000.00 80000.00 99000.00 64400.00 79300.00};
%do i = 61000 %to &amp;amp;median;
proc sql;
create table lala&amp;amp;i as
select count(*)
from singlefamilyhht
where persons=4 and hinc&amp;lt;.8*(i)
or
persons=3 and hinc&amp;lt; .9*(.8*(i))
or
 persons=2 and hinc&amp;lt; .8*(.8*(i))
or
 persons=5 and hinc&amp;lt; 1.08*(.8*(i))
or
persons=6 and hinc&amp;lt; 1.16*(.8*(i))
group by puma1;
quit;
%end;
%mend data;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My log is as follows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&lt;DIV class="sasSource"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;72&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;73 %macro data;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;74 %let median = {61000.00 68000.00 68400.00 88000.00 77400.00 84040.00 103000.00 65400.00 47800.00 75000.00 92000.00&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;74 ! 80000.00 99000.00 64400.00 79300.00};&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;75 %do i = 61000 %to &amp;amp;median;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;76 proc sql;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;77 create table lala&amp;amp;i as&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;78 select count(*)&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;79 from singlefamilyhht&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;80 where persons=4 and hinc&amp;lt;.8*(i)&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;81 or&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;82 persons=3 and hinc&amp;lt; .9*(.8*(i))&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;83 or&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;84 persons=2 and hinc&amp;lt; .8*(.8*(i))&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;85 or&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;86 persons=5 and hinc&amp;lt; 1.08*(.8*(i))&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;87 or&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;88 persons=6 and hinc&amp;lt; 1.16*(.8*(i))&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;89 group by puma1;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;90&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;91 quit;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;92 %end;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;93 %mend data;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;94&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;95 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;DIV class="sasSource"&gt;108&lt;/DIV&gt;&lt;/DIV&gt;&lt;PRE class="sasLog"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;P&gt;Any recommendations?&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 27 Nov 2018 03:10:19 GMT</pubDate>
    <dc:creator>lt1</dc:creator>
    <dc:date>2018-11-27T03:10:19Z</dc:date>
    <item>
      <title>proc sql  / do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-do-loop/m-p/516210#M139387</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a set of numbers that I need to collect the count for. Essentially, I need to perform :&amp;nbsp;&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;proc sql;
create table lala&amp;amp;i as
select count(*)
from singlefamilyhht
where persons=4 and hinc&amp;lt;.8*(i)
or
persons=3 and hinc&amp;lt; .9*(.8*(i))
or
 persons=2 and hinc&amp;lt; .8*(.8*(i))
or
 persons=5 and hinc&amp;lt; 1.08*(.8*(i))
or
persons=6 and hinc&amp;lt; 1.16*(.8*(i))
group by puma1;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;repeatedly for different medians (@i).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I wrote the following code:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro data;
%let median = {61000.00 68000.00 68400.00 88000.00 77400.00 84040.00 103000.00 65400.00 47800.00 75000.00 92000.00 80000.00 99000.00 64400.00 79300.00};
%do i = 61000 %to &amp;amp;median;
proc sql;
create table lala&amp;amp;i as
select count(*)
from singlefamilyhht
where persons=4 and hinc&amp;lt;.8*(i)
or
persons=3 and hinc&amp;lt; .9*(.8*(i))
or
 persons=2 and hinc&amp;lt; .8*(.8*(i))
or
 persons=5 and hinc&amp;lt; 1.08*(.8*(i))
or
persons=6 and hinc&amp;lt; 1.16*(.8*(i))
group by puma1;
quit;
%end;
%mend data;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My log is as follows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&lt;DIV class="sasSource"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;72&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;73 %macro data;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;74 %let median = {61000.00 68000.00 68400.00 88000.00 77400.00 84040.00 103000.00 65400.00 47800.00 75000.00 92000.00&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;74 ! 80000.00 99000.00 64400.00 79300.00};&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;75 %do i = 61000 %to &amp;amp;median;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;76 proc sql;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;77 create table lala&amp;amp;i as&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;78 select count(*)&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;79 from singlefamilyhht&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;80 where persons=4 and hinc&amp;lt;.8*(i)&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;81 or&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;82 persons=3 and hinc&amp;lt; .9*(.8*(i))&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;83 or&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;84 persons=2 and hinc&amp;lt; .8*(.8*(i))&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;85 or&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;86 persons=5 and hinc&amp;lt; 1.08*(.8*(i))&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;87 or&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;88 persons=6 and hinc&amp;lt; 1.16*(.8*(i))&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;89 group by puma1;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;90&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;91 quit;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;92 %end;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;93 %mend data;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;94&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;95 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;DIV class="sasSource"&gt;108&lt;/DIV&gt;&lt;/DIV&gt;&lt;PRE class="sasLog"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;P&gt;Any recommendations?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Nov 2018 03:10:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-do-loop/m-p/516210#M139387</guid>
      <dc:creator>lt1</dc:creator>
      <dc:date>2018-11-27T03:10:19Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql  / do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-do-loop/m-p/516213#M139388</link>
      <description>&lt;P&gt;I suspect you want something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro loop;
  %local i median medians;
  %let medians = 61000.00 68000.00 68400.00 88000.00 77400.00 84040.00 103000.00 65400.00 47800.00 75000.00 92000.00 80000.00 99000.00 64400.00 79300.00;
  %do i = 1 %to %sysfunc(countw(&amp;amp;medians,%str( )));
    %let median=%scan(&amp;amp;medians,&amp;amp;i,%str( ));
    create table LALA&amp;amp;i as 
    select count(*) as COUNT
          ,&amp;amp;median. as MEDIAN&lt;BR /&gt;          ,PUMA1
    from SINGLEFAMILYHHT
    where (PERSONS=2 and HINC &amp;lt;  .80   .8 * &amp;amp;median)
       or (PERSONS=3 and HINC &amp;lt;  .90 * .8 * &amp;amp;median)
       or (PERSONS=4 and HINC &amp;lt; 1.00 * .8 * &amp;amp;median)
       or (PERSONS=5 and HINC &amp;lt; 1.08 * .8 * &amp;amp;median)
       or (PERSONS=6 and HINC &amp;lt; 1.16 * .8 * &amp;amp;median)
    group by PUMA1;
  %end;
%mend ; 
       
proc sql;
  %loop;
quit;
&lt;/CODE&gt;&lt;/PRE&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>Tue, 27 Nov 2018 04:05:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-do-loop/m-p/516213#M139388</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-11-27T04:05:16Z</dc:date>
    </item>
  </channel>
</rss>

