<?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: Drop stock in a month with less than five non-zero returns in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Drop-stock-in-a-month-with-less-than-five-non-zero-returns/m-p/466697#M119114</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/39704"&gt;@MAC1430&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;You've asked for "&lt;EM&gt;millions of observations, so I would appreciate an efficient&amp;nbsp;code&lt;/EM&gt;".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've made the assumption your source data is a SAS table. If so then the data step hash approach will outperform the SQL. Should your source data be in a database then the SQL is the option of choice.&lt;/P&gt;</description>
    <pubDate>Fri, 01 Jun 2018 00:25:15 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2018-06-01T00:25:15Z</dc:date>
    <item>
      <title>Drop stock in a month with less than five non-zero returns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Drop-stock-in-a-month-with-less-than-five-non-zero-returns/m-p/466316#M118974</link>
      <description>&lt;DIV class="lia-message-heading lia-component-message-header"&gt;
&lt;DIV class="lia-quilt-row lia-quilt-row-standard"&gt;
&lt;DIV class="lia-quilt-column lia-quilt-column-20 lia-quilt-column-left"&gt;
&lt;DIV class="lia-quilt-column-alley lia-quilt-column-alley-left"&gt;
&lt;DIV class="lia-message-subject"&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;
&lt;H1&gt;&amp;nbsp;Hi everyone,&lt;/H1&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;DIV id="messagebodydisplay_0" class="lia-message-body"&gt;
&lt;DIV class="lia-message-body-content"&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to drop stocks in a month with less than five non-zero&amp;nbsp;returns. For example&amp;nbsp;stock 1 has 7 daily returns in March 2018 but only three&amp;nbsp;non-zero returns.&amp;nbsp;Therefore, I want to delete&lt;SPAN&gt;&amp;nbsp;daily returns of stock 1 for March 2018&lt;/SPAN&gt;&lt;SPAN&gt;. I have millions of observations, so I would appreciate an efficient&amp;nbsp;code.&amp;nbsp;&lt;/SPAN&gt;Please find the SAS data below.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Thanks a lot for your help.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Best,&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheema&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards expandtabs truncover;
input stock date : yymmdd10. ret ;
format date yymmdd10.;
cards;
1	2018-03-01	0
1	2018-03-02	0.1
1	2018-03-03	0
1	2018-03-04	0.03
1	2018-03-05	0
1	2018-03-06	0.02
1	2018-03-07	0
2	2018-03-01	0.02
2	2018-03-02	0.03
2	2018-03-03	0.04
2	2018-03-04	0.05
2	2018-03-05	0.06
2	2018-03-06	0.07
2	2018-03-07	0.08
2	2018-03-08	0
1	2018-04-01	0.02
1	2018-04-02	0.03
1	2018-04-03	0.04
1	2018-04-04	0.05
1	2018-04-05	0.06
1	2018-04-06	0.07
1	2018-04-07	0.08
2	2018-04-01	0.02
2	2018-04-02	0.03
2	2018-04-03	0.04
2	2018-04-04	0.05
2	2018-04-05	0.06
2	2018-04-06	0.07
2	2018-04-07	0.08
2	2018-04-08	0
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;</description>
      <pubDate>Thu, 31 May 2018 06:12:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Drop-stock-in-a-month-with-less-than-five-non-zero-returns/m-p/466316#M118974</guid>
      <dc:creator>MAC1430</dc:creator>
      <dc:date>2018-05-31T06:12:00Z</dc:date>
    </item>
    <item>
      <title>Re: Drop stock in a month with less than five non-zero returns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Drop-stock-in-a-month-with-less-than-five-non-zero-returns/m-p/466340#M118982</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/39704"&gt;@MAC1430&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;I believe below code should perform reasonably well. I had to set the threshold count to 4 (and not 5) to return some rows using the sample data you've posted.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards expandtabs truncover;
input stock date : yymmdd10. ret ;
format date yymmdd10.;
cards;
1	2018-03-01	0
1	2018-03-02	0.1
1	2018-03-03	0
1	2018-03-04	0.03
1	2018-03-05	0
1	2018-03-06	0.02
1	2018-03-07	0
2	2018-03-01	0.02
2	2018-03-02	0.03
2	2018-03-03	0.04
2	2018-03-04	0.05
2	2018-03-05	0.06
2	2018-03-06	0.07
2	2018-03-07	0.08
2	2018-03-08	0
1	2018-04-01	0.02
1	2018-04-02	0.03
1	2018-04-03	0.04
1	2018-04-04	0.05
1	2018-04-05	0.06
1	2018-04-06	0.07
1	2018-04-07	0.08
2	2018-04-01	0.02
2	2018-04-02	0.03
2	2018-04-03	0.04
2	2018-04-04	0.05
2	2018-04-05	0.06
2	2018-04-06	0.07
2	2018-04-07	0.08
2	2018-04-08	0
;
run;

%let treshold_cnt=4;
data want(drop=_:);

  if _n_=1 then
    do;
      length _cnt 3;
      _cnt=1;
      dcl hash h1(suminc: '_cnt', multidata:'n');
      h1.defineKey('stock','date');
      h1.defineDone();
      do i=1 to nobs;
        set have nobs=nobs point=i;
        date=intnx('month',date,0,'b');
        if ret=0 then h1.ref();
      end;
    end;

    set have;
    h1.sum(key:stock, key:intnx('month',date,0,'b'), sum:_zero_ret_cnt);
    if _zero_ret_cnt&amp;lt;&amp;amp;treshold_cnt then delete;
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 31 May 2018 07:32:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Drop-stock-in-a-month-with-less-than-five-non-zero-returns/m-p/466340#M118982</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2018-05-31T07:32:15Z</dc:date>
    </item>
    <item>
      <title>Re: Drop stock in a month with less than five non-zero returns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Drop-stock-in-a-month-with-less-than-five-non-zero-returns/m-p/466353#M118988</link>
      <description>Thank you very much, it works very well.</description>
      <pubDate>Thu, 31 May 2018 08:05:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Drop-stock-in-a-month-with-less-than-five-non-zero-returns/m-p/466353#M118988</guid>
      <dc:creator>MAC1430</dc:creator>
      <dc:date>2018-05-31T08:05:38Z</dc:date>
    </item>
    <item>
      <title>Re: Drop stock in a month with less than five non-zero returns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Drop-stock-in-a-month-with-less-than-five-non-zero-returns/m-p/466431#M119012</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards expandtabs truncover;
input stock date : yymmdd10. ret ;
month=month(date);
format date yymmdd10.;
cards;
1	2018-03-01	0
1	2018-03-02	0.1
1	2018-03-03	0
1	2018-03-04	0.03
1	2018-03-05	0
1	2018-03-06	0.02
1	2018-03-07	0
2	2018-03-01	0.02
2	2018-03-02	0.03
2	2018-03-03	0.04
2	2018-03-04	0.05
2	2018-03-05	0.06
2	2018-03-06	0.07
2	2018-03-07	0.08
2	2018-03-08	0
1	2018-04-01	0.02
1	2018-04-02	0.03
1	2018-04-03	0.04
1	2018-04-04	0.05
1	2018-04-05	0.06
1	2018-04-06	0.07
1	2018-04-07	0.08
2	2018-04-01	0.02
2	2018-04-02	0.03
2	2018-04-03	0.04
2	2018-04-04	0.05
2	2018-04-05	0.06
2	2018-04-06	0.07
2	2018-04-07	0.08
2	2018-04-08	0
;
run;
proc sql;
create table want as
 select *
  from have
   group by stock,month
    having sum(ret ne 0)&amp;gt;=5 ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 31 May 2018 13:17:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Drop-stock-in-a-month-with-less-than-five-non-zero-returns/m-p/466431#M119012</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-05-31T13:17:52Z</dc:date>
    </item>
    <item>
      <title>Re: Drop stock in a month with less than five non-zero returns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Drop-stock-in-a-month-with-less-than-five-non-zero-returns/m-p/466695#M119113</link>
      <description>Thanks you very much for ksharp :). Its even short code and more efficient.</description>
      <pubDate>Fri, 01 Jun 2018 00:21:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Drop-stock-in-a-month-with-less-than-five-non-zero-returns/m-p/466695#M119113</guid>
      <dc:creator>MAC1430</dc:creator>
      <dc:date>2018-06-01T00:21:36Z</dc:date>
    </item>
    <item>
      <title>Re: Drop stock in a month with less than five non-zero returns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Drop-stock-in-a-month-with-less-than-five-non-zero-returns/m-p/466697#M119114</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/39704"&gt;@MAC1430&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;You've asked for "&lt;EM&gt;millions of observations, so I would appreciate an efficient&amp;nbsp;code&lt;/EM&gt;".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've made the assumption your source data is a SAS table. If so then the data step hash approach will outperform the SQL. Should your source data be in a database then the SQL is the option of choice.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Jun 2018 00:25:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Drop-stock-in-a-month-with-less-than-five-non-zero-returns/m-p/466697#M119114</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2018-06-01T00:25:15Z</dc:date>
    </item>
    <item>
      <title>Re: Drop stock in a month with less than five non-zero returns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Drop-stock-in-a-month-with-less-than-five-non-zero-returns/m-p/466698#M119115</link>
      <description>Dear Patrick,&lt;BR /&gt;&lt;BR /&gt;Thank you for pointing out this one. Yes, my source data is a SAS table, so I I will use data setp hash approach them. Thanks again to both of you, its really helpful &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Fri, 01 Jun 2018 00:33:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Drop-stock-in-a-month-with-less-than-five-non-zero-returns/m-p/466698#M119115</guid>
      <dc:creator>MAC1430</dc:creator>
      <dc:date>2018-06-01T00:33:58Z</dc:date>
    </item>
    <item>
      <title>Re: Drop stock in a month with less than five non-zero returns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Drop-stock-in-a-month-with-less-than-five-non-zero-returns/m-p/466699#M119116</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/39704"&gt;@MAC1430&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;If the SQL performs still good enough then I'd go for the SQL as besides of runtimes it's also always about code maintenance - and the SQL is certainly easier to understand and maintain.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Jun 2018 00:41:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Drop-stock-in-a-month-with-less-than-five-non-zero-returns/m-p/466699#M119116</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2018-06-01T00:41:29Z</dc:date>
    </item>
    <item>
      <title>Re: Drop stock in a month with less than five non-zero returns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Drop-stock-in-a-month-with-less-than-five-non-zero-returns/m-p/466700#M119117</link>
      <description>Yes, SQL is quite easier to understand and make changes. I will check the run-times of both SQL and hash, I guess hash will outperform since my data-set is  100 millions observations.</description>
      <pubDate>Fri, 01 Jun 2018 00:44:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Drop-stock-in-a-month-with-less-than-five-non-zero-returns/m-p/466700#M119117</guid>
      <dc:creator>MAC1430</dc:creator>
      <dc:date>2018-06-01T00:44:45Z</dc:date>
    </item>
  </channel>
</rss>

