BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
kfbaker0206
Fluorite | Level 6
Data DupA
   set Duplicates /*I sorted out all of the duplicates I don't want to do that*/
if first.id= 1 then 'AD RSN'n="h";
if last.id = 1 then 'AD RSN'n="h"
run;

Hello,

 

I have two things that I would like to address. First, I want to see if my dataset has a duplicate, then if it does I want it to change another variable (AD RSN) to the character "H". Please see the before info and after. I sorted out all of the dups but I would prefer not to do that. I would prefer sas code. I am new to sas so I need layman terms please. Please see code.

This is what I have right now:

Name

ID

AD RSN

A

12345

I

 

12345

 

B

67890

I

 

67890

 

C

25489

I

 

25489

 

*~*~*~*~*~**~*~*~*~*~*~*~*~*

This is what I want it to look like: If there is a dup ID, then change both AD_RSN to H.

Name

ID

AD RSN

A

12345

H

 

12345

H

B

67890

H

 

67890

H

C

25489

H

 

25489

H

 

Your response will be greatly appreciated.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

You have two issues here.  First is that the key variables you are inspecting (first.id and last.id) don't exist.  To create them, you need to add the BY statement to the DATA step:

 

data dupA;

set duplicates;

by id;

 

Then you need the right logic to use first.id and last.id to assign values to AD RSN.  This would be one way, which also works if you have more than 2 observations per id:

 

data dupA;

set duplicates;

by id;

if first.id=0 or last.id=0 then 'AD RSN'n = "h";

run;

 

Learning about this topic is important.  You will use these tools over and over again.

 

Finally, "h" and "H" are different.  So pick out the value that you really want.

View solution in original post

2 REPLIES 2
Astounding
PROC Star

You have two issues here.  First is that the key variables you are inspecting (first.id and last.id) don't exist.  To create them, you need to add the BY statement to the DATA step:

 

data dupA;

set duplicates;

by id;

 

Then you need the right logic to use first.id and last.id to assign values to AD RSN.  This would be one way, which also works if you have more than 2 observations per id:

 

data dupA;

set duplicates;

by id;

if first.id=0 or last.id=0 then 'AD RSN'n = "h";

run;

 

Learning about this topic is important.  You will use these tools over and over again.

 

Finally, "h" and "H" are different.  So pick out the value that you really want.

kfbaker0206
Fluorite | Level 6

You are awesome! Thanks so much!

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 2 replies
  • 647 views
  • 0 likes
  • 2 in conversation