<?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: Proc SQL Query for 1st, 2nd and last purchase dates in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-Query-for-1st-2nd-and-last-purchase-dates/m-p/368884#M65002</link>
    <description>&lt;P&gt;It has to be done via PROC SQL???&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This can be done via a very simple data step after the data is extracted from Oracle.&lt;/P&gt;</description>
    <pubDate>Tue, 20 Jun 2017 18:59:36 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2017-06-20T18:59:36Z</dc:date>
    <item>
      <title>Proc SQL Query for 1st, 2nd and last purchase dates</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-Query-for-1st-2nd-and-last-purchase-dates/m-p/368868#M64999</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a table with customer transaction history and I am trying to write a query which brings back these variables:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Customer’s First Purchase date&lt;/LI&gt;
&lt;LI&gt;Customer’s Second Purchase date&lt;/LI&gt;
&lt;LI&gt;Customer’s Last Purchase Date&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;So, I start with a table (transaction) which looks like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="220"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="90"&gt;&lt;STRONG&gt;Customer_ID&lt;/STRONG&gt;&lt;/TD&gt;
&lt;TD width="130"&g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BODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And I am looking for a result which looks like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="520"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="119"&gt;&lt;STRONG&gt;Customer_ID&lt;/STRONG&gt;&lt;/TD&gt;
&lt;TD width="129"&gt;&lt;STRONG&gt;First_purchase_Date&lt;/STRONG&gt;&lt;/TD&gt;
&lt;TD width="150"&gt;&lt;STRONG&gt;Second_purchase_Date&lt;/STRONG&gt;&lt;/TD&gt;
&lt;TD width="122"&gt;&lt;STRONG&gt;last_purchase_date&lt;/STRONG&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;124&lt;/TD&gt;
&lt;TD&gt;12/20/2014&lt;/TD&gt;
&lt;TD&gt;3/15/2016&lt;/TD&gt;
&lt;TD&gt;6/14/2017&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;333&lt;/TD&gt;
&lt;TD&gt;8/5/2013&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;8/5/2013&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;656&lt;/TD&gt;
&lt;TD&gt;1/20/2011&lt;/TD&gt;
&lt;TD&gt;4/16/2014&lt;/TD&gt;
&lt;TD&gt;6/14/2017&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;233&lt;/TD&gt;
&lt;TD&gt;4/30/2010&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;4/30/2010&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;454&lt;/TD&gt;
&lt;TD&gt;10/22/2010&lt;/TD&gt;
&lt;TD&gt;11/5/2012&lt;/TD&gt;
&lt;TD&gt;11/5/2012&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have to query the data from an Oracle Environment. I was able to figure out how to write a query which pulls in the first purchase date (see below ) but I’m having trouble figuring how to incorporate/add/pull the second purchase date and the last purchase date variables. &amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql;
connect to oracle;
create table   want   as
select
customer_id,
datepart(First_purchase_Date) as First_purchase_Date format=date9.
from connection to oracle (
select
t.customer_id,
t.transaction_date AS First_purchase_Date
from transaction t
  inner join  
(select   
customer_id,
min(transaction_date) as First_purchase_Date
from sas_transaction 
where   sales_credit in (1) 
and    business_unit in (3)         
group by customer_id
having max(case when sales_credit in (1) 
and transaction_date &amp;gt;= '01JAN2010'
and transaction_date  &amp;lt;= '20JUN2017'   then 1 else 0 end)=1 ) f     
on   (t.customer_id = f.customer_id 
and   t.transaction_date = f.First_purchase_Date)
where t.business_unit in (3)
and t.sales_credit in (1)  
group by t.First_purchase_Date, t.transaction_date  );
disconnect from oracle;
quit;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any assistance will be greatly aperciated.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Jun 2017 18:42:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-Query-for-1st-2nd-and-last-purchase-dates/m-p/368868#M64999</guid>
      <dc:creator>RobertNYC</dc:creator>
      <dc:date>2017-06-20T18:42:15Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL Query for 1st, 2nd and last purchase dates</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-Query-for-1st-2nd-and-last-purchase-dates/m-p/368884#M65002</link>
      <description>&lt;P&gt;It has to be done via PROC SQL???&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This can be done via a very simple data step after the data is extracted from Oracle.&lt;/P&gt;</description>
      <pubDate>Tue, 20 Jun 2017 18:59:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-Query-for-1st-2nd-and-last-purchase-dates/m-p/368884#M65002</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2017-06-20T18:59:36Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL Query for 1st, 2nd and last purchase dates</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-Query-for-1st-2nd-and-last-purchase-dates/m-p/368890#M65003</link>
      <description>&lt;P&gt;Hi PaigeMiller,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It’s preferable for it to be done in the SQL statement but if you have a way to do it using a data step I’m totally game.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Tue, 20 Jun 2017 19:09:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-Query-for-1st-2nd-and-last-purchase-dates/m-p/368890#M65003</guid>
      <dc:creator>RobertNYC</dc:creator>
      <dc:date>2017-06-20T19:09:25Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL Query for 1st, 2nd and last purchase dates</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-Query-for-1st-2nd-and-last-purchase-dates/m-p/368903#M65004</link>
      <description>&lt;P&gt;My experience with extracting data from relational databases like Oracle is that you are better off doing math like this (and also means, sums, counts, standard deviations, &lt;EM&gt;etc.&lt;/EM&gt;) outside of SQL. Better off? Yes, in terms of speed, in terms of code simplicity, as well as probably for other reasons.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;UNTESTED CODE (assumes data is sorted by customer and date)&lt;/P&gt;
&lt;PRE&gt;data first second last;
    set oracle_extract;
    by customer date;
    if first.customer then purchase_count=0;
    purchase_count+1;
    if purchase_count=1 then output first;
    else if purchase_count=2 then output second;
    if last.customer then output last;
run;&lt;/PRE&gt;
&lt;P&gt;Once you have the three output data sets, FIRST, SECOND and LAST, it's easy to merge them to produce the final&amp;nbsp;data set&amp;nbsp;you described above.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Jun 2017 19:40:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-Query-for-1st-2nd-and-last-purchase-dates/m-p/368903#M65004</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2017-06-20T19:40:36Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL Query for 1st, 2nd and last purchase dates</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-Query-for-1st-2nd-and-last-purchase-dates/m-p/368930#M65006</link>
      <description>&lt;P&gt;It is much better off doing this in datastep and code&amp;nbsp;by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;is&amp;nbsp;much cleaner and I think will be efficient too. But you may able to get the answer by sql with below code. Sorry I just saw that you are running this query in an explicit pass through and sql query I gave will not in explicit pass through as it has sas compoenents in it. Below query should work in explicit pass through.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data abc;
input transaction_id transaction_date:mmddyy10.;
format transaction_date date9.;
datalines;
124 6/14/2017
656 6/14/2017
124 4/17/2017
656 7/23/2016
656 4/17/2016
124 3/15/2016
124 12/20/2014
656 4/16/2014
333 8/5/2013
454 11/5/2012
656 1/20/2011
454 10/22/2010
233 4/30/2010
;

proc sql;
select distinct bde.transaction_id,
       first_transaction_date ,
       second_transaction_date ,
     last_transaction_date 
 from 
(select a.transaction_id, first_transaction_date, last_transaction_date from 
(select transaction_id, transaction_date as first_transaction_date from abc
group by transaction_id
having transaction_date = min(transaction_date))a
inner join
(select transaction_id, transaction_date as last_transaction_date from abc
group by transaction_id
having transaction_date = max(transaction_date))b
on a.transaction_id =b.transaction_id)bde
left join
(select a.transaction_id, min(a.transaction_date) as second_transaction_date from
(select transaction_id, transaction_date  from
abc)a
inner join
(select transaction_id, min(transaction_date)as min_trasaction_date  from abc b
group by transaction_id)b
on a.transaction_id = b.transaction_id
and a.transaction_date &amp;gt; min_trasaction_date)cvcv
on bde.transaction_id =cvcv.transaction_id;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Jun 2017 23:57:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-Query-for-1st-2nd-and-last-purchase-dates/m-p/368930#M65006</guid>
      <dc:creator>kiranv_</dc:creator>
      <dc:date>2017-06-20T23:57:57Z</dc:date>
    </item>
  </channel>
</rss>

