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.
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.
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
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.
"sunny" is actually "Sunny".
Thanks to all!
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.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.