BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Chrishi
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
Imroze
Fluorite | Level 6

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;

View solution in original post

2 REPLIES 2
Imroze
Fluorite | Level 6

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;

Chrishi
Calcite | Level 5

Ya missed out on $ before the format creation thanks.

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 4912 views
  • 0 likes
  • 2 in conversation