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.
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.
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.
You are awesome! Thanks so much!
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.