<?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 improve PROC SQL in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-improve-PROC-SQL/m-p/687024#M208546</link>
    <description />
    <pubDate>Sun, 27 Sep 2020 12:46:51 GMT</pubDate>
    <dc:creator>chriscpa</dc:creator>
    <dc:date>2020-09-27T12:46:51Z</dc:date>
    <item>
      <title>How to improve PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-improve-PROC-SQL/m-p/687023#M208545</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am using PROC SQL to capture the mean of past period returns (please see below). The code that I used (adapted from&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76464"&gt;@s_lassen&lt;/a&gt;'s) works perfectly for monthly data . However, it cannot handle the daily data well due to the much larger size of the dataset (see attached screenshot). In particular, I would like to obtain the mean exret of the same weekday (and different weekday) from weeks t-20 to t-6 . It has been running for more than 20 hours, but has not given the output. Is there any way that I can improve the effeciency of the PROC SQL procedure in this case?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Chris&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table return_lag_620
  as select main.*, 
     (select mean(exret)  from return3 as b 
       where gcode=main.gcode 
         and year=main.year
		 and weekday=main.weekday
         and week between main.week - 20 and main.week - 6)                
       as mean_same_weekday,  
	(select mean(exret)  from return3 as b 
       where gcode=main.gcode 
         and year=main.year
		 and weekday ne main.weekday
         and week between main.week - 20 and main.week - 6)                
       as mean_different_weekday 
  from return3 as main
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 27 Sep 2020 12:26:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-improve-PROC-SQL/m-p/687023#M208545</guid>
      <dc:creator>chriscpa</dc:creator>
      <dc:date>2020-09-27T12:26:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to improve PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-improve-PROC-SQL/m-p/687024#M208546</link>
      <description />
      <pubDate>Sun, 27 Sep 2020 12:46:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-improve-PROC-SQL/m-p/687024#M208546</guid>
      <dc:creator>chriscpa</dc:creator>
      <dc:date>2020-09-27T12:46:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to improve PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-improve-PROC-SQL/m-p/687026#M208548</link>
      <description>&lt;P&gt;Please post example data in usable form (data step with datalines). We can't test code against pictures.&lt;/P&gt;</description>
      <pubDate>Sun, 27 Sep 2020 12:55:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-improve-PROC-SQL/m-p/687026#M208548</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-09-27T12:55:11Z</dc:date>
    </item>
    <item>
      <title>Re: How to improve PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-improve-PROC-SQL/m-p/687029#M208549</link>
      <description>Try Hash Table or other skill. SQL is not suited for big data.</description>
      <pubDate>Sun, 27 Sep 2020 13:03:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-improve-PROC-SQL/m-p/687029#M208549</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-09-27T13:03:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to improve PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-improve-PROC-SQL/m-p/687031#M208550</link>
      <description>&lt;P&gt;Hi Kurt,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your reply. Please find the sample data below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Kind regards,&lt;/P&gt;&lt;P&gt;Chris&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE class="xis-codeFragment"&gt;data input_table;
   input gcode $ weekday $ week $ year $ exret $;
   datalines;&lt;BR /&gt;14d1 5 36 2018 0.3057138508&lt;BR /&gt;14d1 6 36 2018 0.0115518923&lt;BR /&gt;14d1 5 37 2018 -0.018730594&lt;BR /&gt;14d1 6 37 2018 -0.072964828&lt;BR /&gt;14d1 5 38 2018 -0.120781876&lt;BR /&gt;14d1 6 38 2018 0.023588247&lt;BR /&gt;14d1 5 39 2018 -0.062521853&lt;BR /&gt;14d1 6 39 2018 -0.050833541&lt;BR /&gt;14d1 5 40 2018 -0.070952766&lt;BR /&gt;14d1 6 40 2018 0.0297087239&lt;BR /&gt;14d1 5 41 2018 -0.020763611&lt;BR /&gt;14d1 6 41 2018 -0.036628995&lt;BR /&gt;14d1 5 42 2018 -0.122045662&lt;BR /&gt;14d1 6 42 2018 0.0889606272&lt;BR /&gt;14d1 5 43 2018 0.0291037632&lt;BR /&gt;14d1 6 43 2018 -0.005378995&lt;BR /&gt;14d1 5 44 2018 -0.039277301&lt;BR /&gt;14d1 6 44 2018 -0.022922855&lt;BR /&gt;14d1 5 45 2018 -0.005378995&lt;BR /&gt;14d1 6 45 2018 0.0512247782&lt;BR /&gt;14d1 5 46 2018 0.0134889291&lt;BR /&gt;14d1 6 46 2018 -0.005378995&lt;BR /&gt;14d1 5 47 2018 -0.023897514&lt;BR /&gt;14d1 6 47 2018 -0.005378995&lt;BR /&gt;14d1 5 48 2018 -0.024609765&lt;BR /&gt;14d1 6 48 2018 -0.005378995&lt;BR /&gt;14d1 5 49 2018 0.0523133123&lt;BR /&gt;14d1 6 49 2018 -0.023560814&lt;BR /&gt;14d1 5 50 2018 -0.039277301&lt;BR /&gt;14d1 6 50 2018 -0.075554434&lt;BR /&gt;14d1 5 51 2018 0.0686950786&lt;BR /&gt;14d1 6 51 2018 0.0463451425
;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 27 Sep 2020 13:24:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-improve-PROC-SQL/m-p/687031#M208550</guid>
      <dc:creator>chriscpa</dc:creator>
      <dc:date>2020-09-27T13:24:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to improve PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-improve-PROC-SQL/m-p/687047#M208552</link>
      <description>&lt;P&gt;According to SAS query optimization guidelines, subqueries should be replaced by joins when possible. Such as in:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data return3;
   input gcode $ weekday week year exret;
   datalines;
14d1 5 36 2018 0.3057138508
14d1 6 36 2018 0.0115518923
14d1 5 37 2018 -0.018730594
14d1 6 37 2018 -0.072964828
14d1 5 38 2018 -0.120781876
14d1 6 38 2018 0.023588247
14d1 5 39 2018 -0.062521853
14d1 6 39 2018 -0.050833541
14d1 5 40 2018 -0.070952766
14d1 6 40 2018 0.0297087239
14d1 5 41 2018 -0.020763611
14d1 6 41 2018 -0.036628995
14d1 5 42 2018 -0.122045662
14d1 6 42 2018 0.0889606272
14d1 5 43 2018 0.0291037632
14d1 6 43 2018 -0.005378995
14d1 5 44 2018 -0.039277301
14d1 6 44 2018 -0.022922855
14d1 5 45 2018 -0.005378995
14d1 6 45 2018 0.0512247782
14d1 5 46 2018 0.0134889291
14d1 6 46 2018 -0.005378995
14d1 5 47 2018 -0.023897514
14d1 6 47 2018 -0.005378995
14d1 5 48 2018 -0.024609765
14d1 6 48 2018 -0.005378995
14d1 5 49 2018 0.0523133123
14d1 6 49 2018 -0.023560814
14d1 5 50 2018 -0.039277301
14d1 6 50 2018 -0.075554434
14d1 5 51 2018 0.0686950786
14d1 6 51 2018 0.0463451425
;

proc sql;
create table return_lag_620 as
select 
    a.gcode, a.year, a.week, a.weekday, a.exret,
    mean(case when a.weekday=b.weekday then b.exret else . end) as mean_same_weekday,
    mean(case when a.weekday=b.weekday then . else b.exret end) as mean_diff_weekday
from
    return3 as a left join 
    return3 as b on 
        a.gcode=b.gcode and a.year=b.year and b.week between a.week-20 and a.week-6
group by a.gcode, a.year, a.week, a.weekday, a.exret;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 419px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/49844iBF1DBEE30F80F42A/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 27 Sep 2020 16:43:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-improve-PROC-SQL/m-p/687047#M208552</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-09-27T16:43:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to improve PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-improve-PROC-SQL/m-p/687070#M208570</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;subqueries should be replaced by joins when possible&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This!&lt;/P&gt;</description>
      <pubDate>Sun, 27 Sep 2020 22:58:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-improve-PROC-SQL/m-p/687070#M208570</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-09-27T22:58:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to improve PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-improve-PROC-SQL/m-p/687073#M208572</link>
      <description>Thanks PGStats,&lt;BR /&gt;It only took 48 secs to produce the nice output from more than 2 millions rows.&lt;BR /&gt;Cheers,&lt;BR /&gt;Chris&lt;BR /&gt;</description>
      <pubDate>Sun, 27 Sep 2020 23:54:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-improve-PROC-SQL/m-p/687073#M208572</guid>
      <dc:creator>chriscpa</dc:creator>
      <dc:date>2020-09-27T23:54:08Z</dc:date>
    </item>
  </channel>
</rss>

