Hi,
I am working with a data set where I have created new variables based on success of outcomes. 0=not sucessful 1=successful .= outcome not applicable to client. I want to create a 2 new variables count_1 and count_0 that each count the number of 1s and 0s from the outcomes variables so I can eventually calculate a success rate for each client. Thanks in advance for the help.
data outcome;
input client outcome1 outcome2 outcome3;
cards;
A . . 0
B . 1 0
C 0 0 1
D . . 1
;
run;
In a datastep
data want;
set have;
count_1= sum( of outcome: );
count_0 = n(of outcome: ) - count_1;
run;
the outcome: with the colon is a variable list of all of the variables, with the data step functions such as sum, mean, n, std etc then
sum ( of outcome:) is the sum of all the non-missing values. Which with 1/0 coded variables is the same as the count of 1.
The N function returns the number of non-missing values.
BTW mean(of outcome: ) will the be percent of 1's .
In a datastep
data want;
set have;
count_1= sum( of outcome: );
count_0 = n(of outcome: ) - count_1;
run;
the outcome: with the colon is a variable list of all of the variables, with the data step functions such as sum, mean, n, std etc then
sum ( of outcome:) is the sum of all the non-missing values. Which with 1/0 coded variables is the same as the count of 1.
The N function returns the number of non-missing values.
BTW mean(of outcome: ) will the be percent of 1's .
Very neat and elegant sir @ballardw
FWIW an ordinary solution
data want;
set outcome;
count_1=countc(cats(of outcome:),'1');
count_0=countc(cats(of outcome:),'0');
run;
Hello @JenMMerc
Welcome to SAS communities. May i request you to mark BallardW's solution as answered and close the thread. Thank you!
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.