BookmarkSubscribeRSS Feed
BoboTheFool
Calcite | Level 5

I have a single variable in a dataset that has y/n values and need to be changed to 1/0 values.  I have tried and if/then statement, if/then do, and I cannot get anything to work.  Does anyone have any suggestions?

4 REPLIES 4
LinusH
Tourmaline | Level 20

IF/THEN/ELSE is one way.

If you wish to have your 1/0 as NUM, be sure to store it in a new variable.

What problems are you experienceing?

Data never sleeps
Shmuel
Garnet | Level 18

As Y/N are characters you need to:

    If flag = "Y" then flag = "1" else flag = "0";

 

Also you can create a format to display "Y" as "1" and "N" as "0", and eliminate change of data. 

Kurt_Bremser
Super User

First of all, you cannot change the variable "in place", as it is of type character and will stay that way.

There are several ways, all involve replacing the old variable with a new one:

data want;
set have (rename=(variable=oldvar));
if oldvar = 'N' then variable = 0; else variable = 1;
drop oldvar;
run;

or (to take care of missing values):

data want;
set have (rename=(variable=oldvar));
select (oldvar);
  when ('Y') variable = 1;
  when ('N') variable = 0;
  otherwise;
end;
drop oldvar;
run;

or you might create a custom invalue informat with proc format, and use that in an input() function:

proc format;
invalue infmt
  'Y' = 1
  'N' = 0
  other = .
;
run;

data want2;
set have (rename=(variable=oldvar));
variable = input(oldvar,infmt.);
drop oldvar;
run;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

As others have noted, format is probably the simplest in the this instance:

proc format;
  value $yn
    "Y"=1
    "N"=0;
run;

data want;
  set have;
  format ynvariable $yn.;
run;

Personally I tend to avoid proprietary file formats - catalogs for instance all broke due to the 32bit/64bit split.  So in most coding cases I would have one character field, possibly one numeric field (if the data can be numeric) and one coded field.  Then you have the original data and converted data and it can all be plain file.  

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 4 replies
  • 14209 views
  • 2 likes
  • 5 in conversation