DATA Step, Macro, Functions and more

converting character values to numeric values with informat statement

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

converting character values to numeric values with informat statement

Hi,

I'll preface this by saying that I am new to macros.  I want to use them because I am working with a sas data set that has over 100 variables, and roughly 40 of them have Yes/No values that I need to convert.  I want to convert these Yes/No values to 0/1 values, and I'm having a lot of trouble getting the informat to work.  I've created a macro variable (&YesNovars) that contains all of the variables I want to convert.  The data step below runs without error, but when I look at the data, nothing has changed.  Any help is greatly appreciated!

Here is what I've tried:

proc format library=library;

   invalue $yesno 'Y'='1'

                 'N'='0'

                      ;

run;

data bicc.maternal12;

set bicc.maternal10;

attrib &YesNovars informat=$yesno.;

run;

Thanks!


Accepted Solutions
Solution
‎06-03-2013 12:46 PM
Respected Advisor
Posts: 3,156

Re: converting character values to numeric values with informat statement

Informat does not work on SAS tables like that, as they already have their informat and format. Try use Array() to simply your task:

data have;

A='Y'; B='N';run;

data want;

  set have;

  array t a--b;

  do over t;

  if t='Y' then t='1'; else if t='N' then t='0';

  end;

run;

Haikuo

update: if you intend to use the existing macro variable (which I believe you don't have to), replace a--b with &YesNovars.

View solution in original post


All Replies
Solution
‎06-03-2013 12:46 PM
Respected Advisor
Posts: 3,156

Re: converting character values to numeric values with informat statement

Informat does not work on SAS tables like that, as they already have their informat and format. Try use Array() to simply your task:

data have;

A='Y'; B='N';run;

data want;

  set have;

  array t a--b;

  do over t;

  if t='Y' then t='1'; else if t='N' then t='0';

  end;

run;

Haikuo

update: if you intend to use the existing macro variable (which I believe you don't have to), replace a--b with &YesNovars.

New Contributor
Posts: 2

Re: converting character values to numeric values with informat statement

It worked! Thank you!

Super User
Super User
Posts: 7,054

Re: converting character values to numeric values with informat statement

If you want to convert the character variables with Y/N to numeric variables with 1/0 then you will need to give them new names.

data want ;

set have;

array _ch &yesnovars ;

array _num &binaryvars;

do i=1 to dim(_ch);

  if _ch(i)='Y' then _num(i)=1;

  else if _ch(i)='N' then _num(i)=0;

  else _num(i)=.;

end;

run;

N/A
Posts: 1

Re: converting character values to numeric values with informat statement

Thank you, Tom.

It really helps Smiley Happy

🔒 This topic is solved and locked.

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

Discussion stats
  • 4 replies
  • 2180 views
  • 3 likes
  • 4 in conversation