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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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