## Count and Sum Function

Frequent Contributor
Posts: 144

# Count and Sum Function

Hi all—

I’m trying to use base SAS to count points possible and points earned. When counting points possible missing values do not count. I have been trying to use the count function. Is that correct? Your help is appreciated.

data want (keep=S1 S2 S3 S4 S5);

set have;

Points_ Possible=count(S1, S2, S3, S4 ,S5); Not

Total_ points=sum(S1, S2, S3, S4 ,S5);

run;

What I need:

 Id_Survey S1 S2 S3 S4 S5 Points_ Possible Total_ points 4545 1.0 0.0 1.0 1.0 0.0 5.0 3.0 999 0.0 1.0 . . 1.0 3.0 2.0 784 . 0.5 0.0 1.0 0.0 4.0 1.5 852 0.5 . 1.0 . . 2.0 1.5

Thanks!!!

Super User
Posts: 23,776

## Re: Count and Sum Function

Use the n() function

data want (keep=S1 S2 S3 S4 S5 Points_possible total_points);

set have;

Points_ Possible=n(S1, S2, S3, S4 ,S5);

Total_ points=sum(S1, S2, S3, S4 ,S5);

run;

PROC Star
Posts: 8,167

## Re: Count and Sum Function

There are a number of ways to do what you want.  I prefer something like the following, as it is easy to modify for different surveys:

data have;

input Id_Survey S1 S2 S3 S4 S5;

cards;

4545 1.0 0.0 1.0 1.0 0.0 5.0 3.0

999 0.0 1.0 . . 1.0 3.0 2.0

784 . 0.5 0.0 1.0 0.0 4.0 1.5

852 0.5 . 1.0 . . 2.0 1.5

;

data want;

set have;

array s(*) s:;

Points_Possible=dim(s)-nmiss(of S;

Total_points=sum(of S;

run;

Super User
Posts: 6,785

## Re: Count and Sum Function

Perhaps even:

points_possible = n(of S;

Discussion stats
• 3 replies
• 253 views
• 0 likes
• 4 in conversation