Hi,
I'll preface this by saying that I am new to macros. I want to use them because I am working with a sas data set that has over 100 variables, and roughly 40 of them have Yes/No values that I need to convert. I want to convert these Yes/No values to 0/1 values, and I'm having a lot of trouble getting the informat to work. I've created a macro variable (&YesNovars) that contains all of the variables I want to convert. The data step below runs without error, but when I look at the data, nothing has changed. Any help is greatly appreciated!
Here is what I've tried:
proc format library=library;
invalue $yesno 'Y'='1'
'N'='0'
;
run;
data bicc.maternal12;
set bicc.maternal10;
attrib &YesNovars informat=$yesno.;
run;
Thanks!
Informat does not work on SAS tables like that, as they already have their informat and format. Try use Array() to simply your task:
data have;
A='Y'; B='N';run;
data want;
set have;
array t a--b;
do over t;
if t='Y' then t='1'; else if t='N' then t='0';
end;
run;
Haikuo
update: if you intend to use the existing macro variable (which I believe you don't have to), replace a--b with &YesNovars.
Informat does not work on SAS tables like that, as they already have their informat and format. Try use Array() to simply your task:
data have;
A='Y'; B='N';run;
data want;
set have;
array t a--b;
do over t;
if t='Y' then t='1'; else if t='N' then t='0';
end;
run;
Haikuo
update: if you intend to use the existing macro variable (which I believe you don't have to), replace a--b with &YesNovars.
It worked! Thank you!
If you want to convert the character variables with Y/N to numeric variables with 1/0 then you will need to give them new names.
data want ;
set have;
array _ch &yesnovars ;
array _num &binaryvars;
do i=1 to dim(_ch);
if _ch(i)='Y' then _num(i)=1;
else if _ch(i)='N' then _num(i)=0;
else _num(i)=.;
end;
run;
Thank you, Tom.
It really helps
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 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.