Solved
Contributor
Posts: 44

# At least two observations for each ID

[ Edited ]

I have a dataset named "X' with many observations. This is just a sample dataset. I need atleast two observations for each unique ID. For example my dataset looks like below:

ID          date           volume

03          jun1996     100

04          jul1996      110

04          jul1996      120

04          aug1996    130

05          jun1996     105

06           jul1996      110

06           jul1996      110

07          Jun1996    120

08          Jun1998    130

My output should look like below:

04          jul1996      110

04          jul1996      120

04          aug1996    130

06           jul1996      110

06           jul1996      110

How do I code in SAS in order to get an output with atleast two observations per ID.

Accepted Solutions
Solution
‎05-16-2016 03:17 PM
Super User
Posts: 23,724

## Re: At least two observations for each ID

You can use the NOUNIKEY option in PROC SORT as well.

``````proc sort data=have nounikey out=want;
by id;
run;

proc print data=want;
run;``````

All Replies
Posts: 5,529

## Re: At least two observations for each ID

Use SQL:

``````data have;
input ID date \$ volume;
datalines;
03          jun1996     100
04          jul1996      110
04          jul1996      120
04          aug1996    130
05          jun1996     105
06           jul1996      110
06           jul1996      110
07          Jun1996    120
08          Jun1998    130
;

proc sql;
create table want as
select *
from have
group by ID
having count(*) > 1;
select * from want;
quit;
``````
PG
Solution
‎05-16-2016 03:17 PM
Super User
Posts: 23,724

## Re: At least two observations for each ID

You can use the NOUNIKEY option in PROC SORT as well.

``````proc sort data=have nounikey out=want;
by id;
run;

proc print data=want;
run;``````
Contributor
Posts: 44

## Re: At least two observations for each ID

Thank you Reeza. This code helped me to solve my issue.
Posts: 1,250

## Re: At least two observations for each ID

Or use a data step (assuming that dataset HAVE is sorted by ID):

``````data want;
set have;
by id;
if ~(first.id & last.id);
run;``````

Advantage (over PROC SQL without ORDER BY clause): The order of observations within the BY groups is preserved.

Disadvantage (shared with the PROC SORT approach): If you change your specifications from "at least two" to, say, "at least three observations per ID," there is more to change in the code than just ">1" into ">2".

Super User
Posts: 10,778

## Re: At least two observations for each ID

Or DOW:

``````data have;
input ID date \$ volume;
datalines;
03          jun1996     100
04          jul1996      110
04          jul1996      120
04          aug1996    130
05          jun1996     105
06           jul1996      110
06           jul1996      110
07          Jun1996    120
08          Jun1998    130
;
run;

data want;
_n_=0;
do until(last.id);
set have;
by id;
_n_+1;
end;
do until(last.id);
set have;
by id;
if _n_ gt 1 then output;
end;
run;``````
🔒 This topic is solved and locked.