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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15

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

4 REPLIES 4
Haikuo
Onyx | Level 15

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.

Tom
Super User Tom
Super User

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;

Hyoun
Calcite | Level 5

Thank you, Tom.

It really helps Smiley Happy

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
  • 4 replies
  • 5411 views
  • 3 likes
  • 4 in conversation