DATA Step, Macro, Functions and more

How to Print the Observations that Come after N dates?

Accepted Solution Solved
Reply
Contributor
Posts: 29
Accepted Solution

How to Print the Observations that Come after N dates?

This is along the same vein as: https://communities.sas.com/t5/General-SAS-Programming/How-to-Print-Observations-that-Come-after-a-c... (this addressed how to print observations that came after N numerical units).

 

I'm looking to print the observations that come after N distinct dates.

 

For example, I would want this data set to print out all observations above 1 distinct/unique date.

 

data  have;
input id   date $  PaidAmount : dollar.;
format      PaidAmount dollar.;
cards;
1           1/1/15         $1000           
1           1/1/15         $500
2           1/4/15         $300
2           1/5/15         $2000
2           1/6/15         $0
1 1/2/15 $0
; run;

 

I would want the following printed out:

 

2           1/5/15         $2000
2           1/6/15         $0
1 1/2/15 $0

Thanks in advance for your help!

 

John


Accepted Solutions
Solution
‎10-26-2015 09:50 AM
Super User
Posts: 5,505

Re: How to Print the Observations that Come after N dates?

Posted in reply to johnjinkim

I think this is what you are looking for.  First, change your INPUT statement to convert dates properly:

 

input id date : mmddyy8. PaidAmount : dollar.;

 

Then prepare a data set with the proper observations:

 

proc sort data=have;

   by id date;

run;

 

data want;

   set have;

   by id date;

   if first.id then n_dates_found=0;

   if first.date then n_dates_found + 1;

   if n_dates_found > 1;

   format date mmddyys10.;

run;

 

The order would change ... the data set is now in order by ID.  Does that represent a problem?

 

On another note, I would recommend using four-digit years if at all possible.  Depending on the range of your dates, two-digit years risk SAS assigning the wrong century to your dates.

View solution in original post


All Replies
Respected Advisor
Posts: 3,156

Re: How to Print the Observations that Come after N dates?

[ Edited ]
Posted in reply to johnjinkim

Here is a quickie, if you don't care about the row order,

 

proc sql;
 select * from have
 group by id
 having date > min(date)
/*Order by date*/
;
quit;

You could remove the comment, and get something identical to what you want, under the assumption that your original data was sorted by date. 

Regular Contributor
Posts: 212

Re: How to Print the Observations that Come after N dates?

[ Edited ]
Posted in reply to johnjinkim

Hi mate,

 

Use the first. option to set the first occurency to flag = 1 or flag = "true"

or

Use the last. option to set the first occurency to flag = 1 or flag = "true":

Do the following:

 

proc sort data=have;
by date;
run;

data want;
set have;
by date;
if first.date then flg =1;
else flg=0;
run;

data want2;
set want;
where flg=1;
run;

Hope this helps

 

Att

Solution
‎10-26-2015 09:50 AM
Super User
Posts: 5,505

Re: How to Print the Observations that Come after N dates?

Posted in reply to johnjinkim

I think this is what you are looking for.  First, change your INPUT statement to convert dates properly:

 

input id date : mmddyy8. PaidAmount : dollar.;

 

Then prepare a data set with the proper observations:

 

proc sort data=have;

   by id date;

run;

 

data want;

   set have;

   by id date;

   if first.id then n_dates_found=0;

   if first.date then n_dates_found + 1;

   if n_dates_found > 1;

   format date mmddyys10.;

run;

 

The order would change ... the data set is now in order by ID.  Does that represent a problem?

 

On another note, I would recommend using four-digit years if at all possible.  Depending on the range of your dates, two-digit years risk SAS assigning the wrong century to your dates.

Contributor
Posts: 29

Re: How to Print the Observations that Come after N dates?

Posted in reply to Astounding

Exactly what I was looking for! Thanks to everyone for your feedback as it gave me much more insight into the various solutions. Apologies for any clarity issues on my end.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 279 views
  • 3 likes
  • 4 in conversation