DATA Step, Macro, Functions and more

How to convert Y/N data to 0/1 data?

Reply
Occasional Contributor
Posts: 6

How to convert Y/N data to 0/1 data?

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?

Super User
Posts: 5,431

Re: How to convert Y/N data to 0/1 data?

Posted in reply to BoboTheFool

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
Trusted Advisor
Posts: 1,572

Re: How to convert Y/N data to 0/1 data?

Posted in reply to BoboTheFool

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. 

Super User
Posts: 7,821

Re: How to convert Y/N data to 0/1 data?

Posted in reply to BoboTheFool

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;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Super User
Posts: 7,971

Re: How to convert Y/N data to 0/1 data?

Posted in reply to BoboTheFool

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.  

Ask a Question
Discussion stats
  • 4 replies
  • 514 views
  • 1 like
  • 5 in conversation