DATA Step, Macro, Functions and more

Sum a variable if other variables have a spesific combination or if they match

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 12
Accepted Solution

Sum a variable if other variables have a spesific combination or if they match

[ Edited ]

I hope someone can help me. I have a large data sets with four variables. The first 3 variables have duplicate rows where only the last variable is a different number. How do I sum the last variable for all the combinations of the first three variables? So if the first 3 variables as in B_CODE P_CODE and DATE have the same combination I want to sum the the ACTUAL variable.


Accepted Solutions
Solution
‎01-30-2018 02:10 PM
Super User
Posts: 6,753

Re: Sum a variable if other variables have a spesific combination or if they match

Posted in reply to HelpPlease

I think this is what you are asking for.  It creates a SAS data set holding the totals of the last variable, for every possible combination of the first three:

 

proc summary data=have nway;

class var1 var2 var3;

var var4;

output out=want (drop=_type_ _freq_) sum=total_var4;

run;

 

The program requires some tweaking if it is possible that any of your first three variables ever take on a missing value.

View solution in original post


All Replies
SAS Moderator
Posts: 61

Re: Sum a variable if other variables have a spesific combination or if they match [how to improve your question]

Posted in reply to HelpPlease

Hello @HelpPlease,


Your question requires more details before experts can help. Can you revise your question to include more information? 

 

Review this checklist:

  • Specify a meaningful subject line for your topic.  Avoid generic subjects like "need help," "SAS query," or "urgent."
  • When appropriate, provide sample data in text or DATA step format.  See this article for one method you can use.
  • If you're encountering an error in SAS, include the SAS log or a screenshot of the error condition. Use the Photos button to include the image in your message.
    use_buttons.png
  • It also helps to include an example (table or picture) of the result that you're trying to achieve.

To edit your original message, select the "blue gear" icon at the top of the message and select Edit Message.  From there you can adjust the title and add more details to the body of the message.  Or, simply reply to this message with any additional information you can supply.

 

edit_post.png

SAS experts are eager to help -- help them by providing as much detail as you can.

 

This prewritten response was triggered for you by fellow SAS Support Communities member @draycut

.
Super User
Posts: 23,677

Re: Sum a variable if other variables have a spesific combination or if they match

Posted in reply to HelpPlease

I think you're going to need to post some sample data so we understand what you want. 

Ideally include what you have, what you want, and what you've tried.

 


HelpPlease wrote:

I hope someone can help me. I have a large data sets with four variables. The first 3 variables have duplicate rows where only the last variable is a different number. How do I sum the last variable for all the combinations of the first three variables? So if the first 3 variables are equal I want to sum the last variable.


 

Solution
‎01-30-2018 02:10 PM
Super User
Posts: 6,753

Re: Sum a variable if other variables have a spesific combination or if they match

Posted in reply to HelpPlease

I think this is what you are asking for.  It creates a SAS data set holding the totals of the last variable, for every possible combination of the first three:

 

proc summary data=have nway;

class var1 var2 var3;

var var4;

output out=want (drop=_type_ _freq_) sum=total_var4;

run;

 

The program requires some tweaking if it is possible that any of your first three variables ever take on a missing value.

Occasional Contributor
Posts: 12

Re: Sum a variable if other variables have a spesific combination or if they match

Posted in reply to Astounding

Thank you I really appreciate it, it worked!

Super User
Posts: 23,677

Re: Sum a variable if other variables have a spesific combination or if they match

Posted in reply to HelpPlease

https://github.com/statgeek/SAS-Tutorials/blob/master/proc_means_basic.sas

 

Please don't post PDF of images. Post text data - if your problem is more complex you're essentially asking someone to type your data to help you out.

☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 143 views
  • 0 likes
  • 4 in conversation