Hi i need help in replacing the values from below code with yes or no....if the value = I don't drink alcohol then replace it with no, otherwise replace it with yes..
data have; input policy_no Alcohol & $80.; datalines; 1 I don't drink alcohol 2 Less than 5 drinks 3 Between 5 and 21 drinks ;
Replacing it is a bad idea. You actually lose information. Instead, create a new variable. One way:
data want;
set have;
length alcohol_yn $ 3;
if alcohol = "I don't drink alcohol" then alcohol_yn = "no";
else if alcohol > " " then alcohol_yn = "yes";
run;
Notice that the new variable will be left blank if the original variable is blank. Also note that comparisons require an exact match ... spelling, capitalization, punctuation.
Replacing it is a bad idea. You actually lose information. Instead, create a new variable. One way:
data want;
set have;
length alcohol_yn $ 3;
if alcohol = "I don't drink alcohol" then alcohol_yn = "no";
else if alcohol > " " then alcohol_yn = "yes";
run;
Notice that the new variable will be left blank if the original variable is blank. Also note that comparisons require an exact match ... spelling, capitalization, punctuation.
You may want to test on policy_no:. I assume you will be getting data from elsewhere, so you probably need to handle the case where no reply is given.
data have; input policy_no Alcohol & $80.;
if policy_no = . then alcohol = 'unknown';
else if policy_no = 1 then alcohol = 'no';
else alcohol = 'yes';
datalines; 1 I don't drink alcohol 2 Less than 5 drinks 3 Between 5 and 21 drinks ;
If you wan a new variable, using an informat seems to be a good idea:
proc format;
invalue $alcoIn (upcase)
' ' = ' '
"I DON'T DRINK ALCOHOL" = 'no'
other = 'yes'
;
run;
data want;
set have;
length alcohol_yn $3;
alcohol_yn = input(alcohol, $alcoIn.);
run;
Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.
Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.
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.