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

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,829

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
Posts: 1,826

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: 9,600

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
How to convert datasets to data steps
How to post code
Super User
Posts: 9,211

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.

Discussion stats
• 4 replies
• 831 views
• 1 like
• 5 in conversation