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

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;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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 .

 

View solution in original post

4 REPLIES 4
ballardw
Super User

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 .

 

novinosrin
Tourmaline | Level 20

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;

 

 

JenMMerc
Fluorite | Level 6
It worked! Thank you.


novinosrin
Tourmaline | Level 20

Hello @JenMMerc 

 

Welcome to SAS communities. May i request you to mark BallardW's solution as answered and close the thread. Thank you!

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

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!

Register now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1258 views
  • 2 likes
  • 3 in conversation