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

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. 

Please guide me

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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;

View solution in original post

5 REPLIES 5
PGStats
Opal | Level 21

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
Reeza
Super User

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;
danwarags
Obsidian | Level 7
Thank you Reeza. This code helped me to solve my issue.
FreelanceReinh
Jade | Level 19

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".

Ksharp
Super User

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 2147 views
  • 7 likes
  • 5 in conversation