<?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: help with data step. find consecutive days that a condition is met for the card. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/help-with-data-step-find-consecutive-days-that-a-condition-is/m-p/686096#M208137</link>
    <description>&lt;PRE&gt;data want;
   set transaction;
   retain cons_days;
   by card_num;
   daydif = dif (tran_date);
   if first.card_num then cons_days =0;
   if amount ge 150 and daydif=1 then cons_days+1;
   else if amount ge 150 then cons_days=1;
   drop daydif;
run;&lt;/PRE&gt;
&lt;P&gt;The dif function returns the the current value - the value from the previous time the statement executes. (Most uses mean do not use the DIF or LAG function in conditional statements).&lt;/P&gt;</description>
    <pubDate>Wed, 23 Sep 2020 15:27:27 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2020-09-23T15:27:27Z</dc:date>
    <item>
      <title>help with data step. find consecutive days that a condition is met for the card.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/help-with-data-step-find-consecutive-days-that-a-condition-is/m-p/686058#M208118</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a dataset below that shows transaction date and amount for the card_num. I need to find how many consecutive days did the card transact with amount of &amp;gt;= 150.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="box-sizing: border-box; color: #333333; font-family: inherit; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 300; letter-spacing: normal; line-height: 1.7142; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px; margin: 0px;"&gt;&lt;FONT style="background-color: #ffffff; box-sizing: border-box;"&gt;I need to create a variable(cons_days) that will track the 1st day the condition is met for the card&amp;nbsp; and increment the counter if the condition is met for the card in the consecutive days. I have included a picture of the desired output.&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="box-sizing: border-box; color: #333333; font-family: inherit; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 300; letter-spacing: normal; line-height: 1.7142; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px; margin: 0px;"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="box-sizing: border-box; color: #333333; font-family: inherit; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 300; letter-spacing: normal; line-height: 1.7142; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px; margin: 0px;"&gt;&lt;FONT style="background-color: #ffffff; box-sizing: border-box;"&gt;I appreciate your help.&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="box-sizing: border-box; color: #333333; font-family: inherit; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 300; letter-spacing: normal; line-height: 1.7142; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px; margin: 0px;"&gt;&lt;FONT style="background-color: #ffffff; box-sizing: border-box;"&gt;Thank you,&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="box-sizing: border-box; color: #333333; font-family: inherit; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 300; letter-spacing: normal; line-height: 1.7142; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px; margin: 0px;"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="box-sizing: border-box; color: #333333; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 300; letter-spacing: normal; line-height: 150%; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px; margin: 0px;"&gt;&lt;FONT style="background-color: #ffffff;"&gt;data transaction;&lt;BR /&gt;input @1 card_num 2.&lt;BR /&gt;&amp;nbsp;&amp;nbsp; @4 amount 3.&lt;BR /&gt;&amp;nbsp;&amp;nbsp; @8 tran_date MMDDYY10. ;&lt;BR /&gt;format tran_date date9.;&lt;BR /&gt;datalines;&lt;BR /&gt;11 100 01/05/2015&lt;BR /&gt;11 150 01/06/2015&lt;BR /&gt;11 200 01/07/2015&lt;BR /&gt;11 250 01/08/2015&lt;BR /&gt;11 250 01/20/2015&lt;BR /&gt;12 150 01/15/2015&lt;BR /&gt;12 300 02/25/2015&lt;BR /&gt;13 100 01/08/2015&lt;BR /&gt;14 400 01/02/2015&lt;BR /&gt;15 100 01/02/2015&lt;BR /&gt;15 150 01/03/2015&lt;BR /&gt;15 150 01/04/2015&lt;BR /&gt;15 150 02/01/2015&lt;BR /&gt;15 250 02/02/2015&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="box-sizing: border-box; color: #333333; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 300; letter-spacing: normal; line-height: 150%; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px; margin: 0px;"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV id="tinyMceEditorP_S__0" class="mceNonEditable lia-copypaste-placeholder"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;P&gt;DESIRED OUTPUT:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="DESIRED_OUTPUT.PNG" style="width: 961px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/49706iCA971E9F8D7BABD4/image-size/large?v=v2&amp;amp;px=999" role="button" title="DESIRED_OUTPUT.PNG" alt="DESIRED_OUTPUT.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 23 Sep 2020 15:03:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/help-with-data-step-find-consecutive-days-that-a-condition-is/m-p/686058#M208118</guid>
      <dc:creator>P_S_</dc:creator>
      <dc:date>2020-09-23T15:03:36Z</dc:date>
    </item>
    <item>
      <title>Re: help with data step. find consecutive days that a condition is met for the card.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/help-with-data-step-find-consecutive-days-that-a-condition-is/m-p/686075#M208129</link>
      <description>&lt;P&gt;First thing, your example data step does not run.&lt;/P&gt;
&lt;P&gt;You do not mention what should happen if the amount drops below the 150 value after being greater.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The following assigns 0 instead of missing for consecutive days and would reset within to 0 within a card_num if the value goes below 150.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data want;
   set transaction;
   retain cons_days;
   by card_num;
   if first.card_num then cons_days =0;
   if amount ge 150 then cons_days+1;
run;&lt;/PRE&gt;
&lt;P&gt;Retain tells SAS to keep the value of the variable across iterations of the data step, so is often encountered for counting across records like this.&lt;/P&gt;
&lt;P&gt;When BY variables are specified SAS creates automatic variables First. and Last. that indicate whether the current value of that variable is the first, or last, of that level and is a 1/0 or true/false value. So you can use that to reset things when the value changes.&lt;/P&gt;</description>
      <pubDate>Wed, 23 Sep 2020 14:55:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/help-with-data-step-find-consecutive-days-that-a-condition-is/m-p/686075#M208129</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-09-23T14:55:22Z</dc:date>
    </item>
    <item>
      <title>Re: help with data step. find consecutive days that a condition is met for the card.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/help-with-data-step-find-consecutive-days-that-a-condition-is/m-p/686087#M208132</link>
      <description>Hi ballardw;&lt;BR /&gt;&lt;BR /&gt;I have fixed the data step code. it should run now.&lt;BR /&gt;&lt;BR /&gt;Thank you for the idea. I  ran your code and it seems to me that the counter for cons_days is simply incrementing for the card if the tran_amt is &amp;gt;= 150.&lt;BR /&gt; I need to increment it only if the condition is met (tran_amt &amp;gt;= 150 ) and it also need to be consecutive day. For eg. IF you look at row 19 of my "desired output" picture, the cons_day is reset to 1 because the date in row 19 is jan 20, 2015 and the previous date for this card is Jan 08, 2015. So it is not continuous (consecutive) day. So we would reset it to 1 rather than increment it to a 4. Hope this makes sense.</description>
      <pubDate>Wed, 23 Sep 2020 15:12:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/help-with-data-step-find-consecutive-days-that-a-condition-is/m-p/686087#M208132</guid>
      <dc:creator>P_S_</dc:creator>
      <dc:date>2020-09-23T15:12:58Z</dc:date>
    </item>
    <item>
      <title>Re: help with data step. find consecutive days that a condition is met for the card.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/help-with-data-step-find-consecutive-days-that-a-condition-is/m-p/686096#M208137</link>
      <description>&lt;PRE&gt;data want;
   set transaction;
   retain cons_days;
   by card_num;
   daydif = dif (tran_date);
   if first.card_num then cons_days =0;
   if amount ge 150 and daydif=1 then cons_days+1;
   else if amount ge 150 then cons_days=1;
   drop daydif;
run;&lt;/PRE&gt;
&lt;P&gt;The dif function returns the the current value - the value from the previous time the statement executes. (Most uses mean do not use the DIF or LAG function in conditional statements).&lt;/P&gt;</description>
      <pubDate>Wed, 23 Sep 2020 15:27:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/help-with-data-step-find-consecutive-days-that-a-condition-is/m-p/686096#M208137</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-09-23T15:27:27Z</dc:date>
    </item>
    <item>
      <title>Re: help with data step. find consecutive days that a condition is met for the card.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/help-with-data-step-find-consecutive-days-that-a-condition-is/m-p/686102#M208141</link>
      <description>This works. The dif function is pretty neat. Thank you ballardw. I appreciate your help.</description>
      <pubDate>Wed, 23 Sep 2020 15:42:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/help-with-data-step-find-consecutive-days-that-a-condition-is/m-p/686102#M208141</guid>
      <dc:creator>P_S_</dc:creator>
      <dc:date>2020-09-23T15:42:54Z</dc:date>
    </item>
  </channel>
</rss>

