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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: