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!

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

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