Help using Base SAS procedures

Count and Sum Function

Reply
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_SurveyS1S2S3S4S5Points_ PossibleTotal_ points
45451.00.01.01.00.05.03.0
9990.01.0..1.03.02.0
784.0.50.01.00.04.01.5
8520.5.1.0..2.01.5

Thanks!!!

Super User
Posts: 19,862

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: 7,492

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 SSmiley Happy;

  Total_points=sum(of SSmiley Happy;

run;

Super User
Posts: 5,516

Re: Count and Sum Function

Perhaps even:

points_possible = n(of SSmiley Happy;

Ask a Question
Discussion stats
  • 3 replies
  • 232 views
  • 0 likes
  • 4 in conversation