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!
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.