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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.