<?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 identify the first record which satisfy certain criteria? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-identify-the-first-record-which-satisfy-certain-criteria/m-p/461590#M117440</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/37783"&gt;@kiranv_&lt;/a&gt;&amp;nbsp;Your Sql approach will require a tweak besides the fact it is not a one pass solution on account of having to remerge and then filer groups. Well, let's suppose the OP is ok with the remerge aka extra pass as SQL seems syntactically easier and neat with no need for a presort.&lt;/P&gt;&lt;P&gt;Consider the following scenario where there are ties in purch_amt&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;1 1/1 100&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;1 1/2 100 /*ties*/&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;FONT color="#000000"&gt;Your code will return both the records, of course you can add a very simple fix like:&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;FONT color="#000000"&gt;proc sql;&lt;BR /&gt;create table want as&lt;BR /&gt;select * from have&lt;BR /&gt;where purch_amt ge 100&lt;BR /&gt;group by cust_id&lt;BR /&gt;&lt;EM&gt;&lt;STRONG&gt;having purch_amt = min(purch_amt) &lt;FONT color="#339966"&gt;and purch_dt=min(purch_dt);&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/EM&gt;&lt;BR /&gt;quit;&lt;BR /&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;FONT color="#000000"&gt;The idea is that OP wants to get the &lt;STRONG&gt;&lt;EM&gt;earliest&lt;/EM&gt; &lt;/STRONG&gt;record of a transaction made amount to 100 $. Just an intuition.&amp;nbsp; Thank you!&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 11 May 2018 16:02:04 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2018-05-11T16:02:04Z</dc:date>
    <item>
      <title>How to identify the first record which satisfy certain criteria?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-identify-the-first-record-which-satisfy-certain-criteria/m-p/461459#M117401</link>
      <description>&lt;P&gt;Hi all,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm not a very advanced SAS user and only use pretty basic coding. But now I need to use do loop to meet certain criteria. Specifically, I have a list of customers which are identified by their cus_id. Each customer has their purchase history during a month, with purchase dates and purchase amounts. Now I want to identify the purchase date on which each customer has reached or gone beyond $100 purchase amount.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data is as below:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;cust_id &amp;nbsp; &amp;nbsp; purch_dt &amp;nbsp; &amp;nbsp; purch_amt&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1/1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 100&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1/3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;30&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1/29 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 200&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1/5 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 20&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1/15 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 65&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1/30 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;190&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1/31 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 10&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For cust_id=1, I want to pick out 1/1, as &amp;nbsp;that's the date when cust_id=1 made total purchase of 100 and thus meets the criteria of "reach or beyond 100". For cust_id=2, I want to pick out 1/30 for that's the date cust_id=2 made total purchase of 275 and thus beyond 100...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;After researching online, I found that I probably would need to use "if...then...else...then...end" and "do until" statements. I've composed a piece of code in SAS, but it has been running for 2 hours so there must be something wrong.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data x;&amp;nbsp;&lt;/P&gt;&lt;P&gt;set y;&amp;nbsp;&lt;/P&gt;&lt;P&gt;by cust_id purch_dt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;retain cust_id purch_dt purch_amt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if fist.cust_id then do;&amp;nbsp;&lt;/P&gt;&lt;P&gt;cnt=1;&amp;nbsp;&lt;/P&gt;&lt;P&gt;total_purch_amt=purch_amt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;end;&amp;nbsp;&lt;/P&gt;&lt;P&gt;else do (until total_purch_amt &amp;gt;=100);&amp;nbsp;&lt;/P&gt;&lt;P&gt;cnt=cnt+1;&amp;nbsp;&lt;/P&gt;&lt;P&gt;total_purch_amt=purch_amt+total_purch_amt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;end;&amp;nbsp;&lt;/P&gt;&lt;P&gt;output;&amp;nbsp;&lt;/P&gt;&lt;P&gt;run;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can anyone please take a look at my code and kindly let me know where I could possibly go wrong?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you so much! Really appreciate that!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 May 2018 03:25:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-identify-the-first-record-which-satisfy-certain-criteria/m-p/461459#M117401</guid>
      <dc:creator>CrystalSmile</dc:creator>
      <dc:date>2018-05-11T03:25:21Z</dc:date>
    </item>
    <item>
      <title>Re: How to identify the first record which satisfy certain criteria?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-identify-the-first-record-which-satisfy-certain-criteria/m-p/461460#M117402</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input cust_id     purch_dt $    purch_amt ;
cards;
1               1/1               100
1               1/3                30
1                1/29             200
2                1/5               20
2                1/15             65
2                 1/30            190
2                 1/31             10
;

data want;
set have;
by cust_id;
if first.cust_id then do;_t=purch_amt;__t=0; end;
else _t+purch_amt;
if _t&amp;gt;=100 then __t+1;
if __t=1 then output;
drop _:;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;You wrote -"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For cust_id=1, I want to pick out 1/29, as &amp;nbsp;that's the date when cust_id=1 made total purchase of 330 and thus beyond 100"&amp;nbsp;&lt;/P&gt;&lt;P&gt;130 on 1/3 is beyond 100 fyi&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In response to your edit, i edited&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if _t&amp;gt;=100 then __t+1;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 11 May 2018 03:29:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-identify-the-first-record-which-satisfy-certain-criteria/m-p/461460#M117402</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-05-11T03:29:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to identify the first record which satisfy certain criteria?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-identify-the-first-record-which-satisfy-certain-criteria/m-p/461468#M117407</link>
      <description>&lt;P&gt;one way to do in sql is&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input cust_id     purch_dt $    purch_amt ;
cards;
1               1/1               100
1               1/3                30
1               1/9                130
1                1/29             200
2                1/5               20
2                1/15             65
2                1/17             165
2                 1/30            190
2                 1/31             10
;

proc sql;
select * from have
where purch_amt ge 100
group by cust_id
having purch_amt = min(purch_amt);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 11 May 2018 04:40:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-identify-the-first-record-which-satisfy-certain-criteria/m-p/461468#M117407</guid>
      <dc:creator>kiranv_</dc:creator>
      <dc:date>2018-05-11T04:40:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to identify the first record which satisfy certain criteria?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-identify-the-first-record-which-satisfy-certain-criteria/m-p/461476#M117412</link>
      <description>&lt;P&gt;Assuming your data is sorted by cust_id and purchase date:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input cust_id     purch_dt $    purch_amt ;
cards;
1               1/1               100
1               1/3                30
1                1/29             200
2                1/5               20
2                1/15             65
2                 1/30            190
2                 1/31             10
;

data want;
do until (last.cust_id);
    set have; by cust_id;
    if total &amp;lt; 100 then do;
        total = sum(total, purch_amt);
        if total &amp;gt;= 100 then output;
        end;
    end;
run;

proc print data=want noobs; run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 11 May 2018 05:20:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-identify-the-first-record-which-satisfy-certain-criteria/m-p/461476#M117412</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-05-11T05:20:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to identify the first record which satisfy certain criteria?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-identify-the-first-record-which-satisfy-certain-criteria/m-p/461590#M117440</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/37783"&gt;@kiranv_&lt;/a&gt;&amp;nbsp;Your Sql approach will require a tweak besides the fact it is not a one pass solution on account of having to remerge and then filer groups. Well, let's suppose the OP is ok with the remerge aka extra pass as SQL seems syntactically easier and neat with no need for a presort.&lt;/P&gt;&lt;P&gt;Consider the following scenario where there are ties in purch_amt&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;1 1/1 100&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;1 1/2 100 /*ties*/&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;FONT color="#000000"&gt;Your code will return both the records, of course you can add a very simple fix like:&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;FONT color="#000000"&gt;proc sql;&lt;BR /&gt;create table want as&lt;BR /&gt;select * from have&lt;BR /&gt;where purch_amt ge 100&lt;BR /&gt;group by cust_id&lt;BR /&gt;&lt;EM&gt;&lt;STRONG&gt;having purch_amt = min(purch_amt) &lt;FONT color="#339966"&gt;and purch_dt=min(purch_dt);&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/EM&gt;&lt;BR /&gt;quit;&lt;BR /&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;FONT color="#000000"&gt;The idea is that OP wants to get the &lt;STRONG&gt;&lt;EM&gt;earliest&lt;/EM&gt; &lt;/STRONG&gt;record of a transaction made amount to 100 $. Just an intuition.&amp;nbsp; Thank you!&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 May 2018 16:02:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-identify-the-first-record-which-satisfy-certain-criteria/m-p/461590#M117440</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-05-11T16:02:04Z</dc:date>
    </item>
  </channel>
</rss>

