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

Hello guys,

I am working on a dataset in which there are duplicate observations to a death record. I used First. statement to find these duplicates.

My question now is: If I wanted to select only the first record for each participant, how do I go about implementing this?

I know I can use the First. And/or Last. statement in SAS but i can't seem to figure this out. My codes are attached below.

proc sort data= ler2;

 by id;

 run;

 data ler3;

 set ler2;

 by id;

 if id ne .;

 if first.id then output;

 run;

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

How is the posted code not doing what you want?

 

Note that you are first removing any observations where ID is missing with the subsetting IF statement.

You are then removing any extra observations per ID so that you keep only the first.

 

Using a subsetting IF statement before testing the FIRST.ID flag could have, in theory, caused a problem as it could have removed the observation where FIRST.ID is true.  But since you are removing all of the observations where ID is missing it doesn't really cause any trouble.

 

Your data step is equivalent to these other forms:

data ler3;
  set ler2;
  by id;
  where id ne . ;
  if first.id then output;
run;

data ler3;
  set ler2;
  by id;
  if first.id and id ne . then output;
run;

data ler3;
  set ler2;
  by id;
  if first.id and id ne . ;
run;


data ler3;
  set ler2;
  by id;
  if id=. or not first.id then delete;
run;

 

 

View solution in original post

2 REPLIES 2
felipe_campos
Fluorite | Level 6

Try removing the missing values

 

proc sort data= ler2;

 by id;

run;

data ler3;

 set ler2 (where=( id ne .));

 by id;

 if first.id then 
    output;

run;

 

 

Tom
Super User Tom
Super User

How is the posted code not doing what you want?

 

Note that you are first removing any observations where ID is missing with the subsetting IF statement.

You are then removing any extra observations per ID so that you keep only the first.

 

Using a subsetting IF statement before testing the FIRST.ID flag could have, in theory, caused a problem as it could have removed the observation where FIRST.ID is true.  But since you are removing all of the observations where ID is missing it doesn't really cause any trouble.

 

Your data step is equivalent to these other forms:

data ler3;
  set ler2;
  by id;
  where id ne . ;
  if first.id then output;
run;

data ler3;
  set ler2;
  by id;
  if first.id and id ne . then output;
run;

data ler3;
  set ler2;
  by id;
  if first.id and id ne . ;
run;


data ler3;
  set ler2;
  by id;
  if id=. or not first.id then delete;
run;

 

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1354 views
  • 0 likes
  • 3 in conversation