Help using Base SAS procedures

How to convert Y/N data to 0/1 data?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

How to convert Y/N data to 0/1 data?

I have multiple variables in my dataset that have Y/N data but I need to convert it to 0/1 data for analysis. Does anyone know a macro or a code that would help with this conversion of the variables with Y/N data?


Accepted Solutions
Solution
‎11-21-2014 12:07 PM
Super User
Posts: 10,538

Re: How to convert Y/N data to 0/1 data?

Since the existing variables are character they can't be made into numeric but new variables with the numeric values can be created.

OR if you read the data from a text file that could be read into numeric.

A custom informat is the easiest.

proc format;

invalue YN (upcase)

'Y' = 1

'N' = 0

other = .; /* this assigns any unexpected characters to missing */

run;

data want;

     set have;

     /* best to create bunch of variables and assigning values with an array*/

     array c < list name of existing variables with YN values here>;

     array New <a list of corresponding new variables here>;

     do I=1 to dim(c);

          New = input(c,YN.);

     end;

run;

If I were reading the data from a text file I would read those values after assigning YN as the informat.

View solution in original post


All Replies
Solution
‎11-21-2014 12:07 PM
Super User
Posts: 10,538

Re: How to convert Y/N data to 0/1 data?

Since the existing variables are character they can't be made into numeric but new variables with the numeric values can be created.

OR if you read the data from a text file that could be read into numeric.

A custom informat is the easiest.

proc format;

invalue YN (upcase)

'Y' = 1

'N' = 0

other = .; /* this assigns any unexpected characters to missing */

run;

data want;

     set have;

     /* best to create bunch of variables and assigning values with an array*/

     array c < list name of existing variables with YN values here>;

     array New <a list of corresponding new variables here>;

     do I=1 to dim(c);

          New = input(c,YN.);

     end;

run;

If I were reading the data from a text file I would read those values after assigning YN as the informat.

Occasional Contributor
Posts: 6

Re: How to convert Y/N data to 0/1 data?

Thank you!

☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 623 views
  • 1 like
  • 2 in conversation