<?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: Count day of last transaction in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Count-day-of-last-transaction/m-p/615938#M18831</link>
    <description>&lt;P&gt;Do refund records always &lt;EM&gt;&lt;STRONG&gt;immediately&lt;/STRONG&gt;&lt;/EM&gt; follow the refunded transaction?&amp;nbsp; Or can there be other transactions between them?&lt;/P&gt;</description>
    <pubDate>Wed, 08 Jan 2020 14:32:19 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2020-01-08T14:32:19Z</dc:date>
    <item>
      <title>Count day of last transaction</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Count-day-of-last-transaction/m-p/615920#M18829</link>
      <description>&lt;P&gt;Hi Guys, can help me with the following scenerio below. I need to derive the number of days for&amp;nbsp;DAY_LAST_TRNS field. If there is a transaction taken place, the calculation of day_last trns field will be MTH_END - TRNS_DTE. However, if there was a refund to net off the previous transaction, we will not consider it as a transaction record. Therefore, we will take the last transaction as the latest record. Please advice what is the code to derive for DAY_LAST_TRNS.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;MTH END&lt;/TD&gt;&lt;TD&gt;TRNS_DTE&lt;/TD&gt;&lt;TD&gt;NAME&lt;/TD&gt;&lt;TD&gt;TRNF_AMT&lt;/TD&gt;&lt;TD&gt;DAY_LAST_TRNS (MTH_END-TRNS_DTE)&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;31May2001&lt;/TD&gt;&lt;TD&gt;04May2001&lt;/TD&gt;&lt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TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Wed, 08 Jan 2020 13:31:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Count-day-of-last-transaction/m-p/615920#M18829</guid>
      <dc:creator>MK2508</dc:creator>
      <dc:date>2020-01-08T13:31:38Z</dc:date>
    </item>
    <item>
      <title>Re: Count day of last transaction</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Count-day-of-last-transaction/m-p/615926#M18830</link>
      <description>&lt;P&gt;A couple of questions:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Are MTH_END and TRNS_DTE both numeric variables? Or is one or both a character variable? Please run PROC CONTENTS on your SAS data set to see.&lt;/LI&gt;
&lt;LI&gt;Does the refund always happen in the row immediately below the original transaction? Or can the refund show up elsewhere?&lt;/LI&gt;
&lt;LI&gt;Does the refund always have the exact same TRNF_AMT except it is negative?&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;Is the variable NAME relevant to this problem?&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Jan 2020 14:22:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Count-day-of-last-transaction/m-p/615926#M18830</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-01-08T14:22:30Z</dc:date>
    </item>
    <item>
      <title>Re: Count day of last transaction</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Count-day-of-last-transaction/m-p/615938#M18831</link>
      <description>&lt;P&gt;Do refund records always &lt;EM&gt;&lt;STRONG&gt;immediately&lt;/STRONG&gt;&lt;/EM&gt; follow the refunded transaction?&amp;nbsp; Or can there be other transactions between them?&lt;/P&gt;</description>
      <pubDate>Wed, 08 Jan 2020 14:32:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Count-day-of-last-transaction/m-p/615938#M18831</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-01-08T14:32:19Z</dc:date>
    </item>
    <item>
      <title>Re: Count day of last transaction</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Count-day-of-last-transaction/m-p/615967#M18841</link>
      <description>&lt;P&gt;I think you are saying:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;For all refunds (negative trnf_amt) you want the DAYS_LAST_TRNS variable to be the refund transaction date minus the transaction date of the most recent non-refunded transaction.&lt;/LI&gt;
&lt;LI&gt;For all other records N_DAYS is just the number of days from transaction to month end.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If your date variables (per&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;'s question) are numeric data values, and if all refunds immediately follow the corresponding transaction (my question), then you can keep track of an intermediate variable (_last_non_refund_date), which you retain across observations.&amp;nbsp; If the current transaction is not a refund, use TRNS_DATE.&amp;nbsp; But if it is, use _last_non_refund_date:&lt;/P&gt;
&lt;P&gt;&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 MTH_END :date9. TRNS_DTE :date9.  NAME :$3. TRNF_AMT  expected_DAY_LAST_TRNS ;
  format trns_dte mth_end date9.;
datalines;
31May2001 04May2001 ABC 10000.000000 27  
31Jan2012 17Jan2012 ABC 3564.000000 14  
31Jan2013 30Jan2013 ABC 3564.000000 1  
31Dec2013 31Dec2013 ABC 4000.000000 0  
31Jan2015 19Jan2015 ABC 4000.000000 12  
29Feb2016 16Feb2016 ABC 4000.000000 13  
28Feb2017 15Feb2017 ABC 4000.000000 13  
28Feb2018 03Feb2018 ABC 2000.000000 25  
31Jul2018 27Jul2018 ABC 2000.000000 4  
31Jan2019 25Jan2019 ABC 2000.000000 6  
30Sep2019 02Sep2019 ABC 5300.000000 28  
30Sep2019 02Sep2019 ABC -5300.000000 248 
run;

data want (drop=_:);
  set have (keep=name);
  by name;

  merge have
        have (firstobs=2 keep=trnf_amt rename=(trnf_amt=_nxt_trnf_amt));

  retain _last_non_ref_date .;
  if first.name then _last_non_ref_date= .;

  if trnf_amt&amp;gt;0 and _nxt_trnf_amt&amp;gt;0 then _last_non_ref_date=trns_dte;

  new_DAY_LAST_TRNS = mth_end - ifn(trnf_amt&amp;gt;0,trns_dte,_last_non_ref_date);

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;I use the MERGE statement to provide a device for looking ahead one record, which is how I determine whether the record-in-hand is going to be refunded.&amp;nbsp; If so, I don't update the _last_non_ref_date variable.&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;LI&gt;I also use the SET statement, to provide a basis against which the BY statement can be used.&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;LI&gt;This program reads in your expected variable, so you can see it side by side with my calculated variable.&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;LI&gt;I repeat - this program assumes refunds always immediately follow the corresponding transaction.&amp;nbsp; Otherwise more programming is required to detect a transaction that will be ultimately refunded.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Unresolved:&amp;nbsp; what do you want if the very first transaction is refunded in the second transaction?&amp;nbsp; In my program the resulting DAYS_LST_TRNS is a missing value for the refund transaction.&lt;/P&gt;</description>
      <pubDate>Wed, 08 Jan 2020 15:50:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Count-day-of-last-transaction/m-p/615967#M18841</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-01-08T15:50:09Z</dc:date>
    </item>
  </channel>
</rss>

