BookmarkSubscribeRSS Feed
RandyStan
Fluorite | Level 6

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

5 REPLIES 5
ballardw
Super User

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.

Shmuel
Garnet | Level 18

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 ?

RandyStan
Fluorite | Level 6

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

ballardw
Super User

@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.

Ksharp
Super User
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-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 771 views
  • 0 likes
  • 4 in conversation