<?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 to do loops in sas? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-do-loops-in-sas/m-p/649055#M194539</link>
    <description>&lt;P&gt;Further to&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;'s comment, I contend that the 2 SQL steps can be grouped, and that the last 2 steps can be removed altogether as they bring no value, and their logic added to the single SQL step.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In other words, the 12 times 6 steps can be reduced to one SQL query. The query will be much faster if the two sources tables are sorted and/or indexed.&lt;/P&gt;</description>
    <pubDate>Wed, 20 May 2020 00:03:23 GMT</pubDate>
    <dc:creator>ChrisNZ</dc:creator>
    <dc:date>2020-05-20T00:03:23Z</dc:date>
    <item>
      <title>How to do loops in sas?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-do-loops-in-sas/m-p/649045#M194530</link>
      <description>&lt;P&gt;Hi! I have the following code for January that gives me a final table for this month. I need to run this code for all 12 months. Every time I start with a new month I retrieve the information from two datasets; sample_returns and sample_stocks. Since it takes some time so run it all I was wondering if there was a simple loop that&amp;nbsp; I could use to make the process faster.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data sample_returns_jan;
set sample_returns;
where month(date)=1;
run;

data sample_stocks_jan;
set sample_stocks;
where month(date)=1;
run;

proc sql;
create table SampleJanuary as 
select i.*, cq.date, cq.ret
from sample_stocks_jan i left join sample_returns_jan cq
on i.date = cq.date and i.bucket=cq.bucket and i.permno=cq.permno;

proc sql;
create table sampleJanuary1 as
select i.*, sum(EW*ret) as EW_ret, sum((sqrt(12)*(volatility))*100) as vol_annual,
sum(((1+ret)**12)-1) as ret_annual
from SampleJanuary i
group by month_bucket;

data sampleJanuary1a;
set samplejanuary1;
SP_annual= sum(ret_annual/vol_annual);
by month_bucket;
run;

/*Keeping only the relevant information*/
data final_January;
set sampleJanuary1a;
keep bucket date EW_ret vol_annual SP_annual;
run;

&lt;/PRE&gt;</description>
      <pubDate>Tue, 19 May 2020 23:17:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-do-loops-in-sas/m-p/649045#M194530</guid>
      <dc:creator>alatriste26</dc:creator>
      <dc:date>2020-05-19T23:17:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to do loops in sas?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-do-loops-in-sas/m-p/649052#M194537</link>
      <description>&lt;P&gt;Unless you have a HUUUUGE data set that your hardware can't handle easily, looping is not necessary and will not speed things up.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;It seems to me that everything you are doing can be done on the entire data set, without splitting them into data sets for each month. Yes, it may take a long time but it will still be faster that way.&lt;/P&gt;</description>
      <pubDate>Tue, 19 May 2020 23:35:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-do-loops-in-sas/m-p/649052#M194537</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-05-19T23:35:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to do loops in sas?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-do-loops-in-sas/m-p/649053#M194538</link>
      <description>&lt;P&gt;put a macro around it and make the month a macro variable and wrap the code in a do loop:&lt;/P&gt;&lt;PRE class="code highlight"&gt;&lt;SPAN class="line"&gt;&lt;SPAN class="k"&gt;%do&lt;/SPAN&gt; month&lt;SPAN class="o"&gt;=&lt;/SPAN&gt;&lt;SPAN class="mi"&gt;1&lt;/SPAN&gt; &lt;SPAN class="k"&gt;%to&lt;/SPAN&gt; &lt;SPAN class="nv"&gt;12&lt;/SPAN&gt;&lt;SPAN class="p"&gt;;&lt;/SPAN&gt; &lt;/SPAN&gt;
:
:
&lt;SPAN class="line"&gt;&lt;SPAN class="k"&gt;%end&lt;/SPAN&gt;&lt;SPAN class="p"&gt;;&lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;just a thought&lt;/P&gt;</description>
      <pubDate>Tue, 19 May 2020 23:38:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-do-loops-in-sas/m-p/649053#M194538</guid>
      <dc:creator>pmbrown</dc:creator>
      <dc:date>2020-05-19T23:38:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to do loops in sas?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-do-loops-in-sas/m-p/649055#M194539</link>
      <description>&lt;P&gt;Further to&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;'s comment, I contend that the 2 SQL steps can be grouped, and that the last 2 steps can be removed altogether as they bring no value, and their logic added to the single SQL step.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In other words, the 12 times 6 steps can be reduced to one SQL query. The query will be much faster if the two sources tables are sorted and/or indexed.&lt;/P&gt;</description>
      <pubDate>Wed, 20 May 2020 00:03:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-do-loops-in-sas/m-p/649055#M194539</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-05-20T00:03:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to do loops in sas?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-do-loops-in-sas/m-p/649155#M194584</link>
      <description>&lt;P&gt;Adding to my original reply and to&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;&amp;nbsp;'s comments&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your original code, with WHERE statements in data steps will require SAS to read through the original data set 12 times (if you do this in a loop). If you do it as one big (non-loop) action, SAS has to read through the original data set only one time.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;AND&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The non-loop solution is a lot less programming than the loop solution.&lt;/P&gt;</description>
      <pubDate>Wed, 20 May 2020 10:35:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-do-loops-in-sas/m-p/649155#M194584</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-05-20T10:35:22Z</dc:date>
    </item>
  </channel>
</rss>

