Dear All
My data set is as follows
Var_A Var_B Date
1 120 02JAN2020
1 120 05JAN2020
231 CH 06FEB2020
231 CH 12FEB2020
231 CH 15FEB2020
139 SELF 15JAN2020
139 SELF 19JAN2020
139 SELF 2 3 07JAN2020
139 SELF 2 3 15FEB2020
139 SELF 41 09FEB2020
141 SELF 4g 15MAR2020
141 SELF 4g 21MAR2020
141 SELF5 14MAR2020
I want to create a new variable, VAR_C, and the data should look like:
Var_A Var_B Date VAR_C
1 120 02JAN2020 120
1 120 05JAN2020 120
231 CH 06FEB2020 CH
231 CH 12FEB2020 CH
231 CH 15FEB2020 CH
139 SELF 15JAN2020 SELF_MAIN
139 SELF 19JAN2020 SELF_MAIN
139 SELF 2 3 07JAN2020 SELF_SUB
139 SELF 2 3 15FEB2020 SELF_SUB
139 SELF 41 09FEB2020 SELF_SUB
141 SELF 4g 15MAR2020 SELF_SUB
141 SELF 4g 21MAR2020 SELF_SUB
141 SELF5 14MAR2020 SELF_SUB
Please describe the rules or logic for creating the values of Var_c.
I can guess that it has something to do with the variable Var_b. Rules involve things like why when Var_b = CH Var_c is CH but when Var_b=SELF then Var_c =SELF_MAIN.
And we would need a list of every single value of Var_b that gets treated one way or the other, and possibly both.
1) The title of tour post does not fit what you want.
You are asking to add a variable not to change name of a variable.
2) By what logic you want to create the new variable?
Why is "SELF" creating "SELF_MAN" and "SELF <number>" creates "SELF_SUB" ?
What would be with other values like "SELF XXX" or "SYYY Z" ?
Is it case sensitive or not ?
The rule is:
If the Var_B is SELF then VAR_C is SELF_MAIN
If Var_B is SELF followed by numbers or alphabets then VAR_C is SELF_SUB
Otherwise, VAR_C = VAR_B
I hope this helps
Thanks
@RandyStan wrote:
The rule is:
If the Var_B is SELF then VAR_C is SELF_MAIN
If Var_B is SELF followed by numbers or alphabets then VAR_C is SELF_SUB
Otherwise, VAR_C = VAR_B
I hope this helps
Thanks
Are the values shown in your example the only ones that ever appear for Var_b?
If you have other values then you need to describe all of them.
data have; input (Var_A Var_B Date) (& $20.); cards; 1 120 02JAN2020 1 120 05JAN2020 231 CH 06FEB2020 231 CH 12FEB2020 231 CH 15FEB2020 139 SELF 15JAN2020 139 SELF 19JAN2020 139 SELF 2 3 07JAN2020 139 SELF 2 3 15FEB2020 139 SELF 41 09FEB2020 141 SELF 4g 15MAR2020 141 SELF 4g 21MAR2020 141 SELF5 14MAR2020 ; data want; set have; if Var_B='SELF' then var_c='SELF_MAIN'; else if Var_B=: 'SELF' then var_c='SELF_SUB'; else var_c=var_b; run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.