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!
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
Check out this tutorial series to learn how to build your own steps in SAS Studio.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.