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

I have a dataset that has some duplicate observations as follows:

 

ID  VAR1 VAR2 VAR3

1      a        .          .

1      a        b        c

2      d        e         f

3      g        .         .

 

var1, var2 and var3 are numeric.

I want to delete the first observation. I use the following code:

 

Proc sort data=have out=want nodupkey;

by ID VAR1;

run;

 

However, this code delete the second observation, instead of the first one.

Any one knows what I should do to delete the first observations?? Thanks in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data want;
 set have;
 by ID VAR1;
 if last.VAR1;
run;

Assuming your data records are sorted in a sequence where you want to keep just the last record with the key value.

 

See the note from @LinusH:

@Ksharp's code works if your rule is "in case of duplicates remove the first observation".
But your sample data illustrates rather a rule that says remove the observation that has missing values on the non id variables. If this is the case, you need to presort your data to be sure to get the desired result. 

View solution in original post

3 REPLIES 3
Ksharp
Super User
data want;
 set have;
 by ID VAR1;
 if last.VAR1;
run;

Assuming your data records are sorted in a sequence where you want to keep just the last record with the key value.

 

See the note from @LinusH:

@Ksharp's code works if your rule is "in case of duplicates remove the first observation".
But your sample data illustrates rather a rule that says remove the observation that has missing values on the non id variables. If this is the case, you need to presort your data to be sure to get the desired result. 

OceanDream
Fluorite | Level 6

Thank you! Keshan!

LinusH
Tourmaline | Level 20
@Ksharp's code works if your rule is "in case of duplictes remove the first observation".
But your sample data illustrates rather a rule that says remove the observation that has missing values on the non id variables. If this is the case, you need to presort your data to be sure to get the desired result.
Data never sleeps

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

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

View all other training opportunities.

Discussion stats
  • 3 replies
  • 14380 views
  • 1 like
  • 3 in conversation