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;
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 25. 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.