I have multiple variables in my dataset that have Y/N data but I need to convert it to 0/1 data for analysis. Does anyone know a macro or a code that would help with this conversion of the variables with Y/N data?
Since the existing variables are character they can't be made into numeric but new variables with the numeric values can be created.
OR if you read the data from a text file that could be read into numeric.
A custom informat is the easiest.
proc format;
invalue YN (upcase)
'Y' = 1
'N' = 0
other = .; /* this assigns any unexpected characters to missing */
run;
data want;
set have;
/* best to create bunch of variables and assigning values with an array*/
array c < list name of existing variables with YN values here>;
array New <a list of corresponding new variables here>;
do I=1 to dim(c);
New = input(c,YN.);
end;
run;
If I were reading the data from a text file I would read those values after assigning YN as the informat.
Since the existing variables are character they can't be made into numeric but new variables with the numeric values can be created.
OR if you read the data from a text file that could be read into numeric.
A custom informat is the easiest.
proc format;
invalue YN (upcase)
'Y' = 1
'N' = 0
other = .; /* this assigns any unexpected characters to missing */
run;
data want;
set have;
/* best to create bunch of variables and assigning values with an array*/
array c < list name of existing variables with YN values here>;
array New <a list of corresponding new variables here>;
do I=1 to dim(c);
New = input(c,YN.);
end;
run;
If I were reading the data from a text file I would read those values after assigning YN as the informat.
Thank you!
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.