<?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: How can I use the do loop for a proc sql Iteration? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-use-the-do-loop-for-a-proc-sql-Iteration/m-p/206080#M38310</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;BR /&gt;Simply calculate week from your data and then use in group by:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data a;&lt;/P&gt;&lt;P&gt; set a;&lt;/P&gt;&lt;P&gt; by id date;&lt;/P&gt;&lt;P&gt; retain _dt;&lt;/P&gt;&lt;P&gt; if first.id then _dt=date;&lt;/P&gt;&lt;P&gt; wk=(date-dt+1)/7;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="background-color: #ffffff; margin: 0px 0px 1em; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; color: #222222; font-size: 15px;"&gt;proc sql;&lt;/P&gt;&lt;P style="background-color: #ffffff; margin: 0px 0px 1em; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; color: #222222; font-size: 15px;"&gt;select count(*) as CNT, &lt;/P&gt;&lt;P style="background-color: #ffffff; margin: 0px 0px 1em; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; color: #222222; font-size: 15px;"&gt;calculated CNT / (select count(distinct id) from table A) as response_rate&lt;/P&gt;&lt;P style="background-color: #ffffff; margin: 0px 0px 1em; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; color: #222222; font-size: 15px;"&gt;from table A as A, table B as B&lt;/P&gt;&lt;P style="background-color: #ffffff; margin: 0px 0px 1em; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; color: #222222; font-size: 15px;"&gt;where A.id = B.id&lt;/P&gt;&lt;P style="background-color: #ffffff; margin: 0px 0px 1em; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; color: #222222; font-size: 15px;"&gt;group by wk;&lt;/P&gt;&lt;P style="background-color: #ffffff; margin: 0px 0px 1em; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; color: #222222; font-size: 15px;"&gt;quit;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 25 Aug 2015 18:46:09 GMT</pubDate>
    <dc:creator>ndp</dc:creator>
    <dc:date>2015-08-25T18:46:09Z</dc:date>
    <item>
      <title>How can I use the do loop for a proc sql Iteration?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-use-the-do-loop-for-a-proc-sql-Iteration/m-p/206075#M38305</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P style="margin: 0 0 1em; font-size: 15px; color: #222222; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; background-color: #ffffff;"&gt;I'm new to SAS macro programming. I did a lot research on this topic, but I still can't figure out how to integrate those good examples into my problem here.&lt;/P&gt;&lt;P style="margin: 0 0 1em; font-size: 15px; color: #222222; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; background-color: #ffffff;"&gt;Problem:&lt;/P&gt;&lt;P style="margin: 0 0 1em; font-size: 15px; color: #222222; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; background-color: #ffffff;"&gt;I have 2 SAS tables to work with, table A and table B.&lt;/P&gt;&lt;P style="margin: 0 0 1em; font-size: 15px; color: #222222; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; background-color: #ffffff;"&gt;table A has a 6 month records. What I need to do is to extract distinct id weekly from this 6 months period.&lt;/P&gt;&lt;P style="margin: 0 0 1em; font-size: 15px; color: #222222; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; background-color: #ffffff;"&gt;Each iteration, I need to perform this procedure as shown below:&lt;/P&gt;&lt;P style="margin: 0 0 1em; font-size: 15px; color: #222222; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; background-color: #ffffff;"&gt;proc sql;&lt;/P&gt;&lt;P style="margin: 0 0 1em; font-size: 15px; color: #222222; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; background-color: #ffffff;"&gt;select distinct id&lt;/P&gt;&lt;P style="margin: 0 0 1em; font-size: 15px; color: #222222; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; background-color: #ffffff;"&gt;from table A&lt;/P&gt;&lt;P style="margin: 0 0 1em; font-size: 15px; color: #222222; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; background-color: #ffffff;"&gt;where date &amp;gt;= '27SEP2013'd and date &amp;lt;= '03OCT2013'd; /* first week of the 6 months period*/&lt;/P&gt;&lt;P style="margin: 0 0 1em; font-size: 15px; color: #222222; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; background-color: #ffffff;"&gt;quit;&lt;/P&gt;&lt;P style="margin: 0 0 1em; font-size: 15px; color: #222222; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; background-color: #ffffff;"&gt;proc sql;&lt;/P&gt;&lt;P style="margin: 0 0 1em; font-size: 15px; color: #222222; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; background-color: #ffffff;"&gt;select count(*) as CNT, &lt;/P&gt;&lt;P style="margin: 0 0 1em; font-size: 15px; color: #222222; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; background-color: #ffffff;"&gt;calculated CNT / (select count(distinct id) from table A) as response_rate&lt;/P&gt;&lt;P style="margin: 0 0 1em; font-size: 15px; color: #222222; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; background-color: #ffffff;"&gt;from table A as A, table B as B&lt;/P&gt;&lt;P style="margin: 0 0 1em; font-size: 15px; color: #222222; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; background-color: #ffffff;"&gt;where A.id = B.id&lt;/P&gt;&lt;P style="margin: 0 0 1em; font-size: 15px; color: #222222; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; background-color: #ffffff;"&gt;quit;&lt;/P&gt;&lt;P style="margin: 0 0 1em; font-size: 15px; color: #222222; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; background-color: #ffffff;"&gt;so I need to do this week by week for about 26 weeks to cover that 6 months period in Table A.&lt;/P&gt;&lt;P style="margin: 0 0 1em; font-size: 15px; color: #222222; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; background-color: #ffffff;"&gt;I don't know how to use the %Do start_date %To end_date %By(week) loop to do this iteration.&lt;/P&gt;&lt;P style="margin: 0 0 1em; font-size: 15px; color: #222222; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; background-color: #ffffff;"&gt;Any help? Thanks.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 24 Aug 2015 18:33:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-use-the-do-loop-for-a-proc-sql-Iteration/m-p/206075#M38305</guid>
      <dc:creator>Dozel</dc:creator>
      <dc:date>2015-08-24T18:33:22Z</dc:date>
    </item>
    <item>
      <title>Re: How can I use the do loop for a proc sql Iteration?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-use-the-do-loop-for-a-proc-sql-Iteration/m-p/206076#M38306</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;Why not change your first SQL to count distinct per week using a group by and WEEK() function?&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;You'll end up with something like the following for Table A&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;Week DistinctID&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 24 Aug 2015 19:05:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-use-the-do-loop-for-a-proc-sql-Iteration/m-p/206076#M38306</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2015-08-24T19:05:01Z</dc:date>
    </item>
    <item>
      <title>Re: How can I use the do loop for a proc sql Iteration?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-use-the-do-loop-for-a-proc-sql-Iteration/m-p/206077#M38307</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Can you post simple example data for A , B and the results.&lt;/P&gt;&lt;P&gt;Not sure why you can't join the two tables and use GROUP BY.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 24 Aug 2015 20:52:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-use-the-do-loop-for-a-proc-sql-Iteration/m-p/206077#M38307</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2015-08-24T20:52:38Z</dc:date>
    </item>
    <item>
      <title>Re: How can I use the do loop for a proc sql Iteration?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-use-the-do-loop-for-a-proc-sql-Iteration/m-p/206078#M38308</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;If you're new to SAS Macro programming then you're at risk to step into the trap to opt too fast for a macro based solution. Only use macro coding if you can't do it without. As Reeza and Tom already are hinting in your case it's highly likely that you don't need macro coding at all but simply need to get your SQL right. If you can provide some sample data and show us how the expected result should look like then I'm sure someone will come up with a way of how to get it done.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 25 Aug 2015 09:56:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-use-the-do-loop-for-a-proc-sql-Iteration/m-p/206078#M38308</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2015-08-25T09:56:07Z</dc:date>
    </item>
    <item>
      <title>Re: How can I use the do loop for a proc sql Iteration?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-use-the-do-loop-for-a-proc-sql-Iteration/m-p/206079#M38309</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;This sco wiki page has coded that explains the macro %do loop using date intervals.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A class="active_link" href="http://www.sascommunity.org/wiki/Macro_Loops_with_Dates" title="http://www.sascommunity.org/wiki/Macro_Loops_with_Dates"&gt;http://www.sascommunity.org/wiki/Macro_Loops_with_Dates&lt;/A&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 25 Aug 2015 14:46:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-use-the-do-loop-for-a-proc-sql-Iteration/m-p/206079#M38309</guid>
      <dc:creator>Ron_MacroMaven</dc:creator>
      <dc:date>2015-08-25T14:46:44Z</dc:date>
    </item>
    <item>
      <title>Re: How can I use the do loop for a proc sql Iteration?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-use-the-do-loop-for-a-proc-sql-Iteration/m-p/206080#M38310</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;BR /&gt;Simply calculate week from your data and then use in group by:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data a;&lt;/P&gt;&lt;P&gt; set a;&lt;/P&gt;&lt;P&gt; by id date;&lt;/P&gt;&lt;P&gt; retain _dt;&lt;/P&gt;&lt;P&gt; if first.id then _dt=date;&lt;/P&gt;&lt;P&gt; wk=(date-dt+1)/7;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="background-color: #ffffff; margin: 0px 0px 1em; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; color: #222222; font-size: 15px;"&gt;proc sql;&lt;/P&gt;&lt;P style="background-color: #ffffff; margin: 0px 0px 1em; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; color: #222222; font-size: 15px;"&gt;select count(*) as CNT, &lt;/P&gt;&lt;P style="background-color: #ffffff; margin: 0px 0px 1em; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; color: #222222; font-size: 15px;"&gt;calculated CNT / (select count(distinct id) from table A) as response_rate&lt;/P&gt;&lt;P style="background-color: #ffffff; margin: 0px 0px 1em; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; color: #222222; font-size: 15px;"&gt;from table A as A, table B as B&lt;/P&gt;&lt;P style="background-color: #ffffff; margin: 0px 0px 1em; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; color: #222222; font-size: 15px;"&gt;where A.id = B.id&lt;/P&gt;&lt;P style="background-color: #ffffff; margin: 0px 0px 1em; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; color: #222222; font-size: 15px;"&gt;group by wk;&lt;/P&gt;&lt;P style="background-color: #ffffff; margin: 0px 0px 1em; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; color: #222222; font-size: 15px;"&gt;quit;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 25 Aug 2015 18:46:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-use-the-do-loop-for-a-proc-sql-Iteration/m-p/206080#M38310</guid>
      <dc:creator>ndp</dc:creator>
      <dc:date>2015-08-25T18:46:09Z</dc:date>
    </item>
  </channel>
</rss>

