BookmarkSubscribeRSS Feed
jloft12
Calcite | Level 5

Hello, 

 

I'm trying to create a new variable in a data set which would act as a scale reflecting the number of positive responses among a group of other variables.

 

So, there are 5 variables with categorical, binary responses (yes/no), and I want to make a new variable which would have 6 categorical responses which might be All Five, Four, Three, Two, One and None. For example, an observation with a 'Yes' response to 2 of the 5 variables would warrant a "Two" in my new variable. Three 'Yes's' would be a Three, and so on. 

 

Making the 'All 5' and 'None' is easy, and I know I could sort of use brute force and set up if-then statements for all the possible combinations for 2, 3, and 4, but I feel there may be a simpler and less error-prone way to go about this in a data statement. Any ideas?

 

Thank you in advance!

3 REPLIES 3
PaigeMiller
Diamond | Level 26

If your binary variables named x1 through x5 are numeric and coded as 0 or 1, this is pretty easy.

 

newvariable=sum(of x1-x5);
--
Paige Miller
DartRodrigo
Lapis Lazuli | Level 10

Hi mate,

 

So you only need to make this new variable about the YES ?

MikeZdeb
Rhodochrosite | Level 12

Hi, if you really want words ...

 

data x;
input x1-x5;
datalines;

. . . . .
0 0 0 0 0
1 0 0 0 0
1 1 0 0 0
1 1 1 0 0
1 1 1 1 0
1 1 1 1 1
;

 

data y;
set x;
score = upcase(put(sum(of x: ),words.));
run;

 

x1 x2 x3 x4 x5 score

 .  .  .  .  . MISSING

 0  0  0  0  0 ZERO
 1  0  0  0  0 ONE
 1  1  0  0  0 TWO
 1  1  1  0  0 THREE
 1  1  1  1  0 FOUR
 1  1  1  1  1 FIVE

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