how to assign 2 different values of a new variable according to the values of an existing variable

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 9
Accepted Solution

how to assign 2 different values of a new variable according to the values of an existing variable

I tried to assign 2 different values (1, 0) of a new variable according to the value of an existing variable.

data test2;

set test;

if target in ("rain", "snow") then target_kgb = "1";

else if target = "sunny" then target_kgb = "0";

run;

 

But I can only assign 1 to target_kgb if target in rain or snow. 0 was not assigned to target_kgb even though there were many "sunny" for target.

 

Any help is highly appreciated.

 

Thank a lot.


Accepted Solutions
Solution
‎05-21-2018 01:36 PM
Super User
Posts: 6,759

Re: how to assign 2 different values of a new variable according to the values of an existing variab

Posted in reply to jeniffer_jacob

Your code is perfect.  Most likely, the data are a little bit different.  For example, none of these would produce a match with "sunny":

 

Sunny

s u n n y

suny

SUNNY

    sunny

 

Character strings must match exactly.

View solution in original post


All Replies
Valued Guide
Posts: 533

Re: how to assign 2 different values of a new variable according to the values of an existing variab

[ Edited ]
Posted in reply to jeniffer_jacob

Check the length of your variable "target". Without anything else it will be defined as $4 because "rain" is the first possible value the datastep finds. it is safer to add a statement

 

length target $5;

before the if-then-else statements. This will prevent "sunny" to be truncated to "sunn" which never matches "sunny". Possibly this is already happening in the creation of dataset test. We would need to see more of your program and logs to know for sure. Do a proc contents on dataset test.

 

Hope this helps,

-- Jan

Solution
‎05-21-2018 01:36 PM
Super User
Posts: 6,759

Re: how to assign 2 different values of a new variable according to the values of an existing variab

Posted in reply to jeniffer_jacob

Your code is perfect.  Most likely, the data are a little bit different.  For example, none of these would produce a match with "sunny":

 

Sunny

s u n n y

suny

SUNNY

    sunny

 

Character strings must match exactly.

Occasional Contributor
Posts: 9

Re: how to assign 2 different values of a new variable according to the values of an existing variab

Posted in reply to Astounding

"sunny" is actually "Sunny". 

 

Thanks to all!

Super Contributor
Posts: 339

Re: how to assign 2 different values of a new variable according to the values of an existing variab

Posted in reply to jeniffer_jacob

Hi,

 

Your data step code worked OK for me when I tried the following data:

 

 

data test;
   input target $8.;
   datalines;
rain
snow
sunny
;

data test2;
   set test;
   if target in ("rain", "snow") then target_kgb = "1";
   else if target = "sunny" then target_kgb = "0";
run;

 

 

Perhaps you have a leading space(s) before "sunny" in the variable target, in which case you could try using:

 

else if left(target) = "sunny" then target_kgb = "0";

 

If that works then I recommend you use left(target) wherever you have target.

 

 

Regards,

Amir.

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 122 views
  • 0 likes
  • 4 in conversation