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

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?

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

2 REPLIES 2
ballardw
Super User

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.

WillS_
Calcite | Level 5

Thank you!

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 2 replies
  • 3527 views
  • 1 like
  • 2 in conversation