BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
johnjinkim
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

4 REPLIES 4
Haikuo
Onyx | Level 15

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. 

DartRodrigo
Lapis Lazuli | Level 10

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

Astounding
PROC Star

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.

johnjinkim
Obsidian | Level 7

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.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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