I have a dataset which contain gender variable which might be given in numeric(0/1) or character(Female/Male). If i get the data in numeric i have to apply format created by me as female or male, if given in character keep it as it is so i was apllyig format using if then else its not working any suggestion on this.
proc format;
value sex 0='Female'1='Male';
run;
data have;
input gender;
cards;
0
0
0
1
1
1
;
run;
data have1;
input gender$;
cards;
Female
Female
Female
Male
Male
Male
;
run;
data want;
set have;
if anydigit(gender) ge 1 then
gender1=put(gender,sex.);
else gender1=gender;
run;
data want1;
set have1;
if anydigit(gender) lt 1 then
gender1=put(gender,sex.);
else gender1=gender;
run;
The problem arises when the data is given in character and i dont know wheather the given data is char or numeric before hand.
Thanks
Please try the below code... hope it helps....
proc format;
value $sex 0='Female'
1='Male'
other='unknown';
run;
data have;
length gender $8.;
input gender $;
cards;
0
0
Female
1
1
Male
;
run;
data want;
set have;
if anydigit(gender) ge 1 then
gender1=put(gender,$sex.);
else gender1=gender;
run;
Please try the below code... hope it helps....
proc format;
value $sex 0='Female'
1='Male'
other='unknown';
run;
data have;
length gender $8.;
input gender $;
cards;
0
0
Female
1
1
Male
;
run;
data want;
set have;
if anydigit(gender) ge 1 then
gender1=put(gender,$sex.);
else gender1=gender;
run;
Ya missed out on $ before the format creation thanks.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.