BookmarkSubscribeRSS Feed
Bipingaud
Calcite | Level 5

hey, iam new to sas , recently stuck to a problem.

i am trying to convert 0 to NO and 1 to YES for a variable but get fails every time  . if plzz can anyone help me out , Thankyou. 

3 REPLIES 3
Reeza
Super User

Ideally you should post what you've tried so we can let you know where your having issues. 

 

This is should be a straightforward if then. 

 

If var=0 the new_var='Yes';

else if var=1 then new_var='No';

else new_var='Other';

Rick_SAS
SAS Super FREQ

The easiest way is to leave the original data alone but use a SAS format to associate 0->"NO" and 1->"YES". See the article 

"Use SAS formats to bin variables":

 

data Have;
input Answer @@;
datalines;
0 0 1 1 1 0 1 0 1
;

proc format;
value YesNo  
      0 = "No"
      1 = "Yes";
run;

proc freq data=Have;
format Answer YesNo.;  /* assign YesNo format */
tables Answer;
run;

 

ballardw
Super User

I'm going to guess that you tried something like:

 

if var=1 then var='yes';

This will not work because the variable VAR is numeric and can not hold character values.

 

To add to @Rick_SAS's suggestion: Formats are very flexible. You could have multiple formats that assign different values to 1/0 and use as needed. Such as True/False  Answered/Not Answered. You could even provide more information such as "Has insurance"/"Does not have insurance", without adding any variable just using a different format for use in a procedure.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 1606 views
  • 5 likes
  • 4 in conversation