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?
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?
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.
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;
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.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.