BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
rangermollie
Calcite | Level 5

Hi there!

 

I have a data set that would be similar to the very basic example below. I'm looking to code so that any ID #'s where there is only one observation (only appear once) get deleted. So in the example below, the Student ID 3140 and its data would be deleted. The rest would be kept. 

 

Student ID               Test Score                Grade

2131                               A                            9

2131                               C                            9

3140                               A                            10

7139                               B                            8

7139                               C                           8

7139                               A                            8

 

 

Thanks so much!

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
proc sql;
    create table want as select * from have
    group by studentID
    having count(studentID)>1;
quit;
--
Paige Miller

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26
proc sql;
    create table want as select * from have
    group by studentID
    having count(studentID)>1;
quit;
--
Paige Miller
rangermollie
Calcite | Level 5
Thank you!!!!
ballardw
Super User

One way

Note data step to create an actual example data set. This is how you should provided example data so we do not have to guess about properties and variable names.

When you use a BY statement in the data step SAS creates automatic variables that indicate whether the current record comes from the first or last observation of the By group. The values are numeric 1/0 which SAS treats as true and false when used in comparisons. You reference the value with First.variablename and Last.variablename (note the dot in there, no spaces allowed). So if a record is both the First and Last of group at the same time there is only one record for that By group. By group processing also want data sorted by default. In this case the sort might not be needed but your real data is likely to be more than 6 records and if all of a student id values are not together this wont' work.

data have;
 input StudentID TestScore $ Grade;
datalines;
2131  A  9
2131  C  9
3140  A  10
7139  B  8
7139  C  8
7139  A  8
;

/* make sure all records for same 
   studentid are together
*/
proc sort data=have;
   by studentid;
run;

data want;
   set have;
   by studentid;
   if first.studentid and last.studentid then delete;
run;

@rangermollie wrote:

Hi there!

 

I have a data set that would be similar to the very basic example below. I'm looking to code so that any ID #'s where there is only one observation (only appear once) get deleted. So in the example below, the Student ID 3140 and its data would be deleted. The rest would be kept. 

 

Student ID               Test Score                Grade

2131                               A                            9

2131                               C                            9

3140                               A                            10

7139                               B                            8

7139                               C                           8

7139                               A                            8

 

 

Thanks so much!


 

PeterClemmensen
Tourmaline | Level 20
data have;
input StudentID TestScore $ Grade;
datalines;
2131 A 9 
2131 C 9 
3140 A 10
7139 B 8 
7139 C 8 
7139 A 8 
;

data want;
   set have;
   by StudentID;
   if first.StudentID and last.StudentID then delete;
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 387 views
  • 0 likes
  • 4 in conversation