BookmarkSubscribeRSS Feed
Eugenio21
Calcite | Level 5

Hi,

 

I am trying to get the oldest date across multiple dates and flag each according to the column title.

 

below is the example data:P

CUSTOMER_KEYSERVICE_KEYFirst_dateSecond_dateThird_dateFourth_date
123456654321..8/14/2012.
1234576543212..1/8/2013.
12345812432103.6/29/2010..
12345918320994..12/15/2017.
12346024209885..2/19/2018.
1234613009877612/19/20113/23/20107/3/2012.
1234623598766710/7/20124/19/20107/12/2012.
1234634187655810/7/201212/1/20107/28/2012.
1234644776544910/7/201210/27/20116/7/2013.
1234655365434010/7/201211/28/20118/13/2013.
1234665954323110/7/201211/28/20116/17/2014.
1234676543212210/7/201211/28/20116/24/2014.
1234687132101310/7/201211/28/20116/30/2014.
1234697720990410/7/201211/28/201111/14/2014.
1234708309879510/7/201211/28/201111/19/2014.
1234718898768610/7/201211/28/201111/21/2014.

 

 

This is the code I was trying to use in proc sql

 

Thank you all for your help!

case

 when first_date < second_date and first_date < third_date and first_date < fourth_date then Service1

 when second_date < first_date and second_date < third_date and second_date < fourth_date then Service2

 when third_date < first_date and third_date < second_date and third_date < fourth_date then Service3

 when fourth_date < first_date and fourth_date < second_date and fourth_date < third_date then Service1

else NonService

end as FirstTrans

 

12 REPLIES 12
novinosrin
Tourmaline | Level 20
data have;
infile cards truncover;
input CUSTOMER_KEY	SERVICE_KEY	(First_date	Second_date	Third_date	Fourth_date) (:mmddyy10.);
format First_date--Fourth_date mmddyy10.;
cards;
123456	654321	.	.	8/14/2012	.
123457	6543212	.	.	1/8/2013	.
123458	12432103	.	6/29/2010	.	.
123459	18320994	.	.	12/15/2017	.
123460	24209885	.	.	2/19/2018	.
123461	30098776	12/19/2011	3/23/2010	7/3/2012	.
123462	35987667	10/7/2012	4/19/2010	7/12/2012	.
123463	41876558	10/7/2012	12/1/2010	7/28/2012	.
123464	47765449	10/7/2012	10/27/2011	6/7/2013	.
123465	53654340	10/7/2012	11/28/2011	8/13/2013	.
123466	59543231	10/7/2012	11/28/2011	6/17/2014	.
123467	65432122	10/7/2012	11/28/2011	6/24/2014	.
123468	71321013	10/7/2012	11/28/2011	6/30/2014	.
123469	77209904	10/7/2012	11/28/2011	11/14/2014	.
123470	83098795	10/7/2012	11/28/2011	11/19/2014	.
123471	88987686	10/7/2012	11/28/2011	11/21/2014	.
;

data want;
set have;
want=min(of first_date--fourth_date);
format want mmddyy10.;
run;
Eugenio21
Calcite | Level 5

Hi Novinosrin,

 

Thank you for the suggestion, what I was trying to do is to name each statement according to what the date is for. 

If first_date is the most recent among all the dates then i want to name it as service 1,

if second_date is the most recent among all the dates then i want to name it as service 2,

and so on,

Eugenio21
Calcite | Level 5
....and add a column to reflect the name
novinosrin
Tourmaline | Level 20

It would be better if you post a sample of your required output

Reeza
Super User

Use MIN(), WHICHN()

 

data want;
set have;

min_date = min(of first_date, second_date, ...);
location_min_date = whichn(x, of first_date, second_date, ...);

run;

What about duplicates though? This will capture only the first value. 

 

If this becomes more of an issue, I would recommend sorting the data and holding two arrays, one with date, one with order. Or flip your data to long. That makes this very easy. 


@Eugenio21 wrote:

Hi,

 

I am trying to get the oldest date across multiple dates and flag each according to the column title.

 

below is the example data:P

CUSTOMER_KEY SERVICE_KEY First_date Second_date Third_date Fourth_date
123456 654321 . . 8/14/2012 .
123457 6543212 . . 1/8/2013 .
123458 12432103 . 6/29/2010 . .
123459 18320994 . . 12/15/2017 .
123460 24209885 . . 2/19/2018 .
123461 30098776 12/19/2011 3/23/2010 7/3/2012 .
123462 35987667 10/7/2012 4/19/2010 7/12/2012 .
123463 41876558 10/7/2012 12/1/2010 7/28/2012 .
123464 47765449 10/7/2012 10/27/2011 6/7/2013 .
123465 53654340 10/7/2012 11/28/2011 8/13/2013 .
123466 59543231 10/7/2012 11/28/2011 6/17/2014 .
123467 65432122 10/7/2012 11/28/2011 6/24/2014 .
123468 71321013 10/7/2012 11/28/2011 6/30/2014 .
123469 77209904 10/7/2012 11/28/2011 11/14/2014 .
123470 83098795 10/7/2012 11/28/2011 11/19/2014 .
123471 88987686 10/7/2012 11/28/2011 11/21/2014 .

 

 

This is the code I was trying to use in proc sql

 

Thank you all for your help!

case

 when first_date < second_date and first_date < third_date and first_date < fourth_date then Service1

 when second_date < first_date and second_date < third_date and second_date < fourth_date then Service2

 when third_date < first_date and third_date < second_date and third_date < fourth_date then Service3

 when fourth_date < first_date and fourth_date < second_date and fourth_date < third_date then Service1

else NonService

end as FirstTrans

 


 

Eugenio21
Calcite | Level 5
Thank you Reeza. ALl the data is unique so there is not duplicates. I ran your code and it did not give me the result i was looking for.

below is the code that I recently use however the nulls are not recognized in the calculation

,case
when first_loan_date < (first_chq_date and first_chq_date is not missing) or first_loan_date < (first_service_date and first_service_date is not missing) or first_loan_date < (first_ex_date and first_ex_date is not missing) then 'FirstTransClt'
ELSE 'None' end as First_Trans
Eugenio21
Calcite | Level 5
...what I am trying to do is to get the oldest date from the four columns of dates and name it as first trans
Reeza
Super User
It should have worked. post the exact code you used and the log please. Also, explain how it does not 'work'. It should have given you 1, 2, 3, etc for the location_min_date and you can then convert that to 'first_trans' or whatever text you want. If it's the variable name you can also use an ARRAY method but that's manual and more work.

Untested:

data want;
set have;
array dates(*) <list your dates variables>;

Min_date = min(of dates(*));
do i=1 to dim(dates);
if dates(i) = min_date then do;
found = vname (dates(i));
leave;
end;
end;

run;
Eugenio21
Calcite | Level 5

hello!

 

this is the example data I have:

CUSTOMER_KEYSERVICE_KEYFirst_Loan_dateFirst_Chq_dateFirst_Service_dateFirst_Ex_date
800001700019/16/201210/6/201111/1/2012.
8000027000210/18/201210/13/201111/15/2012.
8000037000311/1/201210/22/201112/21/2012.
8000047000411/15/20128/27/20121/27/2013.
8000057000511/29/201211/1/20121/24/2014.
8000067000612/13/201211/15/20126/30/2015.
800007700071/10/201311/29/20128/3/2015.
800008700081/27/201312/13/20129/30/2015.
800009700091/27/201312/21/20121/13/2016.
800010700101/27/20131/10/20134/10/2016.
800011700111/27/20131/10/20137/28/2016.
800012700121/27/20131/10/201311/27/2017.
800013700131/27/20131/10/201312/9/2017.
800014700141/27/20131/10/201312/14/2017.
800015700151/27/20131/10/201312/20/2017.
800016700161/27/20131/10/20133/22/2018.
800017700171/27/20131/10/20133/22/2018.
8000187001810/6/20111/9/2015..
8000197001910/26/20111/9/2015..
8000207002011/10/20111/9/2015..
8000217002111/18/20111/9/2015..
8000227002211/25/20111/9/2015..
8000237002310/6/2011...
800024700245/12/201210/6/2011..
800025700256/6/20122/2/2012..
8000267002610/6/2011.4/27/20142/8/2015
8000277002710/28/2011.8/7/20143/18/2015
8000287002810/31/2012.4/15/20158/1/2016
800029700291/3/2013.5/21/20158/1/2016
800030700304/30/2013.8/1/2016

8/1/2016

 

and from that data, I want to produce this table below:

CUSTOMER_KEYSERVICE_KEYFirst_ServiceFirst_Loan_dateFirst_Chq_dateFirst_Service_dateFirst_Ex_date
80000170001Chq9/16/201210/6/201111/1/2012.
80000270002Chq10/18/201210/13/201111/15/2012.
80000370003Chq11/1/201210/22/201112/21/2012.
80000470004Chq11/15/20128/27/20121/27/2013.
80000570005Chq11/29/201211/1/20121/24/2014.
80000670006Chq12/13/201211/15/20126/30/2015.
80000770007Chq1/10/201311/29/20128/3/2015.
80000870008Loan1/27/201212/13/20129/30/2015.
80000970009Loan1/27/201212/21/20121/13/2016.
80001070010Loan1/27/20121/10/20134/10/2016.
80001170011Loan1/27/20121/10/20137/28/2016.
80001270012Loan1/27/20121/10/201311/27/2017.
80001370013Loan1/27/20121/10/201312/9/2017.
80001470014Loan1/27/20121/10/201312/14/2017.
80001570015Loan1/27/20121/10/201312/20/2017.
80001670016Loan1/27/20121/10/20133/22/2018.
80001770017Loan1/27/20121/10/20133/22/2018.
80001870018Loan10/6/20111/9/2015..
80001970019Loan10/26/20111/9/2015..
80002070020Loan11/10/20111/9/2015..
80002170021Loan11/18/20111/9/2015..
80002270022Loan11/25/20111/9/2015..
80002370023Loan10/6/2011...
80002470024Chq5/12/201210/6/2011..
80002570025Chq6/6/20122/2/2012..
80002670026Loan10/6/2011.4/27/20142/8/2015
80002770027Loan10/28/2011.8/7/20143/18/2015
80002870028Loan10/31/2012.4/15/20158/1/2016
80002970029Loan1/3/2013.5/21/20158/1/2016
80003070030Loan4/30/2013.8/1/20168/1/2016

 

...the column 'First_Trans' refers to the oldest date in that particular row.

 

I have used the below case statement in proc sql, however did not give me what I wanted.

 

,case
when lr.first_loan_date < (cr.first_chq_date and sr.first_service_date and er.first_ex_date) then 'Loan'
when cr.first_chq_date < (lr.first_loan_date and sr.first_service_date and er.first_ex_date) then 'Chq'
when sr.first_service_date < (lr.first_loan_date and cr.first_chq_date and er.first_ex_date) then 'Service'
when er.first_ex_date < (lr.first_loan_date and sr.first_service_date and cr.first_chq_date) then 'Currency'
ELSE 'None' end as First_Trans

 

the code above is not generating what I wanted to, below is the result I get:

CUSTOMER_KEYFirst_TransFirst_LoanFirst_Chq_CashedFirst_ServiceFirst_Currency_Exchange
80000001Service10/31/20082/4/2010..
80000002Chq10/31/2008...
80000003Currency10/31/20081/2/20153/30/2012.
80000004Currency10/31/20083/10/20104/30/2012.
80000005Chq10/31/2008...
80000006Currency10/31/20086/12/20144/15/2012.
80000007Chq10/31/2008...
80000008Chq11/1/2008...
80000009Chq11/1/2008...
80000010Service11/1/200811/19/2008..
80000011Chq11/1/2008...
80000012Chq11/1/2008.8/12/2016.
80000013Chq11/1/2008.11/23/2012.
80000014Chq11/1/2008...
80000015Chq11/1/2008.9/12/2013.
80000016Chq11/1/2008.6/19/2012.
80000017Chq11/1/2008...
80000018Service11/1/200811/11/2008..

 

in the first row, it says First_Tran is 'Service', however the First_Service data is blank or null and all rows are like that. The code is not identifying whether the referred date is lover than the rest of the dates in the same row.

 

Thanks for helping.

Eugenio21
Calcite | Level 5
correction...'lower' than the rest of the dates in the same row.
Reeza
Super User
If it’s SQL just keep checking your conditions until you get what’s you want. I wouldn’t do an SQL solution for this type of problem. Your brackets are in the wrong places for your conditions though. And do one at a time so you understand it.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 12 replies
  • 1495 views
  • 0 likes
  • 3 in conversation