SAS Conditional Sum

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

SAS Conditional Sum

I have a program where I want to sum a column if it meets a certain condition.

My dataset looks something like this:

 

ques_type   ques_num  resp1   resp2   resp3   ...   resp50

     4                     1           0          1           1                 1

     3                     2           1          1           0                 1

     2                     3           1          0           1                 1

     1                     4           0          0           0                 1

     3                     5           1          1           0                 0

 

Using the code below I can get the sums for all questions. What I want to do is this, but for each ques_type. So it would have a total for each question type 1, 2, 3, 4, 5. Is there a way to do conditional summing with the proc summary statement?

proc summary data=proj.domains_ques;
   output out=sum sum(resp1-resp50)=sum1-sum50;
   run;

Accepted Solutions
Solution
‎06-14-2016 03:42 PM
Super User
Posts: 10,500

Re: SAS Conditional Sum

Can you show what you expect for results?

 

I think you might want

proc summary data=proj.domains_ques nway;
   class ques_type;
   output out=sum sum(resp1-resp50)=sum1-sum50;
run;

Nway option will only provide output for the levels of the class variable, without that option you would also have an output line with the overall sums.

 

View solution in original post


All Replies
Respected Advisor
Posts: 3,777

Re: SAS Conditional Sum

Make ques_type a CLASS variable.

Solution
‎06-14-2016 03:42 PM
Super User
Posts: 10,500

Re: SAS Conditional Sum

Can you show what you expect for results?

 

I think you might want

proc summary data=proj.domains_ques nway;
   class ques_type;
   output out=sum sum(resp1-resp50)=sum1-sum50;
run;

Nway option will only provide output for the levels of the class variable, without that option you would also have an output line with the overall sums.

 

New Contributor
Posts: 2

Re: SAS Conditional Sum

That worked!
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 386 views
  • 1 like
  • 3 in conversation