Solved
Contributor
Posts: 29

# 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         \$01           1/2/15         \$0;
run;```

I would want the following printed out:

```2           1/5/15         \$2000
2           1/6/15         \$01           1/2/15         \$0```

John

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

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

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.

All Replies
Posts: 3,167

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

[ Edited ]

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: 222

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

[ Edited ]

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: 6,785

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

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