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!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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