<?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 calculating second last date in a table using left join in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/calculating-second-last-date-in-a-table-using-left-join/m-p/299205#M63038</link>
    <description>&lt;P&gt;data test;&lt;BR /&gt;input cust_id trans_date ddmmyy10.;&lt;BR /&gt;cards;&lt;BR /&gt;1 25/08/2016&lt;BR /&gt;1 02/09/2016&lt;BR /&gt;1 28/08/2016&lt;BR /&gt;1 27/08/2016&lt;BR /&gt;1 26/08/2016&lt;BR /&gt;2 02/09/2016&lt;BR /&gt;2 29/09/2016&lt;BR /&gt;2 28/08/2016&lt;BR /&gt;2 01/09/2016&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/*I want to get a column name as sec_date for the above table per customer id, i had used the below codes but it is not working, please help*/&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;create table sample as&lt;BR /&gt;select cust_id&lt;BR /&gt;,min(trans_date) as sec_date&lt;BR /&gt;from test A&lt;BR /&gt;LEFT OUTER JOIN&lt;BR /&gt;(&lt;BR /&gt;select cust_id&lt;BR /&gt;,min(trans_date) as min_date&lt;BR /&gt;from test ) as src&lt;BR /&gt;ON A.cust_id=src.cust_id and A.sec_date &amp;gt; src.min_date&lt;BR /&gt;group by cust_id&lt;BR /&gt;;quit;&lt;/P&gt;</description>
    <pubDate>Mon, 19 Sep 2016 06:00:53 GMT</pubDate>
    <dc:creator>farrukh</dc:creator>
    <dc:date>2016-09-19T06:00:53Z</dc:date>
    <item>
      <title>calculating second last date in a table using left join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculating-second-last-date-in-a-table-using-left-join/m-p/299205#M63038</link>
      <description>&lt;P&gt;data test;&lt;BR /&gt;input cust_id trans_date ddmmyy10.;&lt;BR /&gt;cards;&lt;BR /&gt;1 25/08/2016&lt;BR /&gt;1 02/09/2016&lt;BR /&gt;1 28/08/2016&lt;BR /&gt;1 27/08/2016&lt;BR /&gt;1 26/08/2016&lt;BR /&gt;2 02/09/2016&lt;BR /&gt;2 29/09/2016&lt;BR /&gt;2 28/08/2016&lt;BR /&gt;2 01/09/2016&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/*I want to get a column name as sec_date for the above table per customer id, i had used the below codes but it is not working, please help*/&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;create table sample as&lt;BR /&gt;select cust_id&lt;BR /&gt;,min(trans_date) as sec_date&lt;BR /&gt;from test A&lt;BR /&gt;LEFT OUTER JOIN&lt;BR /&gt;(&lt;BR /&gt;select cust_id&lt;BR /&gt;,min(trans_date) as min_date&lt;BR /&gt;from test ) as src&lt;BR /&gt;ON A.cust_id=src.cust_id and A.sec_date &amp;gt; src.min_date&lt;BR /&gt;group by cust_id&lt;BR /&gt;;quit;&lt;/P&gt;</description>
      <pubDate>Mon, 19 Sep 2016 06:00:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculating-second-last-date-in-a-table-using-left-join/m-p/299205#M63038</guid>
      <dc:creator>farrukh</dc:creator>
      <dc:date>2016-09-19T06:00:53Z</dc:date>
    </item>
    <item>
      <title>Re: calculating second last date in a table using left join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculating-second-last-date-in-a-table-using-left-join/m-p/299206#M63043</link>
      <description>&lt;P&gt;If you want to get the second-to-last date for each customer, just reverse the order of the dates and select the second observation for each by group:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=test;
by cust_id descending trans_date;
run;

data sample (keep=cust_id trans_date rename=(trans_date=sec_date));
set test;
by cust_id;
if first.cust_id
then counter = 1;
else counter + 1;
if counter = 2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 19 Sep 2016 06:21:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculating-second-last-date-in-a-table-using-left-join/m-p/299206#M63043</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-09-19T06:21:14Z</dc:date>
    </item>
    <item>
      <title>Re: calculating second last date in a table using left join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculating-second-last-date-in-a-table-using-left-join/m-p/299207#M63044</link>
      <description>&lt;P&gt;Thanks Kurt .. this works for me !!&lt;/P&gt;</description>
      <pubDate>Mon, 19 Sep 2016 06:33:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculating-second-last-date-in-a-table-using-left-join/m-p/299207#M63044</guid>
      <dc:creator>farrukh</dc:creator>
      <dc:date>2016-09-19T06:33:20Z</dc:date>
    </item>
  </channel>
</rss>

