DATA Step, Macro, Functions and more

Apllying format while using if then else....

Accepted Solution Solved
Reply
Contributor
Posts: 29
Accepted Solution

Apllying format while using if then else....

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


Accepted Solutions
Solution
‎08-25-2014 02:43 AM
Contributor
Posts: 62

Re: Apllying format while using if then else....

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


All Replies
Solution
‎08-25-2014 02:43 AM
Contributor
Posts: 62

Re: Apllying format while using if then else....

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;

Contributor
Posts: 29

Re: Apllying format while using if then else....

Ya missed out on $ before the format creation thanks.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

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