Desktop productivity for business analysts and programmers

Changing a variable column based on another variable

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 12
Accepted Solution

Changing a variable column based on another variable

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.

 


Accepted Solutions
Solution
‎06-27-2018 12:49 PM
Super User
Posts: 6,934

Re: Changing a variable column based on another variable

[ Edited ]
Posted in reply to kfbaker0206

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


All Replies
Solution
‎06-27-2018 12:49 PM
Super User
Posts: 6,934

Re: Changing a variable column based on another variable

[ Edited ]
Posted in reply to kfbaker0206

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.

Occasional Contributor
Posts: 12

Re: Changing a variable column based on another variable

Posted in reply to Astounding

You are awesome! Thanks so much!

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 78 views
  • 0 likes
  • 2 in conversation