10-17-2016 04:18 AM
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?
10-17-2016 04:25 AM
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?
10-17-2016 04:26 AM
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.
10-17-2016 04:27 AM
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;
10-17-2016 04:43 AM
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.