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 2025: Call for Content

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!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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