BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Solly7
Pyrite | Level 9

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
;
1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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.

View solution in original post

3 REPLIES 3
Astounding
PROC Star

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.

DavePrinsloo
Pyrite | Level 9

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 ;

 

andreas_lds
Jade | Level 19

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;

sas-innovate-white.png

Our biggest data and AI event of the year.

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.

 

Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1431 views
  • 2 likes
  • 4 in conversation