sas - Sum values only over replicated dates - Stack Overflow

admin2025-04-28  2

How can I sum values of a variable only when there appear replicated dates per ID?

Suppose to have:

data have;
  input ID :$20. Date :date09. Value; 
  format Date date9. Value;
cards;
0001 13JAN2017 0
0001 22FEB2017 1
0001 22FEB2017 1
0001 30JAN2019 0
0002 28DEC2014 1 
0002 28DEC2014 2
0002 28DEC2014 1
0002 28DEC2014 0
0003 15DEC2021 0
0003 16DEC2021 1
;


The desired output should be:

data want;
  input ID :$20. Date :date09. Value; 
  format Date date9. Value;
cards;
0001 13JAN2017 0
0001 22FEB2017 2
0001 30JAN2019 0
0002 28DEC2014 4
0003 15DEC2021 0
0003 16DEC2021 1
;

Thank you in advance

How can I sum values of a variable only when there appear replicated dates per ID?

Suppose to have:

data have;
  input ID :$20. Date :date09. Value; 
  format Date date9. Value;
cards;
0001 13JAN2017 0
0001 22FEB2017 1
0001 22FEB2017 1
0001 30JAN2019 0
0002 28DEC2014 1 
0002 28DEC2014 2
0002 28DEC2014 1
0002 28DEC2014 0
0003 15DEC2021 0
0003 16DEC2021 1
;


The desired output should be:

data want;
  input ID :$20. Date :date09. Value; 
  format Date date9. Value;
cards;
0001 13JAN2017 0
0001 22FEB2017 2
0001 30JAN2019 0
0002 28DEC2014 4
0003 15DEC2021 0
0003 16DEC2021 1
;

Thank you in advance

Share Improve this question asked Jan 8 at 13:31 NewUsr_statNewUsr_stat 2,5815 gold badges29 silver badges41 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

Just sum over ALL of the groups. The groups that only have one observation are not really any different than those with multiple observations.

proc summary data=have ;
   by id date;
   var value;
   output out=want sum=;
run;
转载请注明原文地址:http://www.anycun.com/QandA/1745854463a91262.html