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!
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);
Hi mate,
So you only need to make this new variable about the YES ?
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
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
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.