Solved
Contributor
Posts: 22

# Sum of data with sorted dates

[ Edited ]

I am trying to sum units based on the start_dt where concent_cnt is 1 to 5

i need a sum of units based on range of dates

Accepted Solutions
Solution
‎10-02-2017 05:39 PM
Posts: 1,229

## Re: Sum based on date range

Basically you need GROUP BY, not ORDER BY:

``````proc sql;
select np_resource_id,start_dt,activity_dt as end_dt,
sum(unit) as total_units,
max(consec_cnt) as final_consec,
end_dt-start_dt as difference
from have
group by np_resource_id,start_dt
having consec_cnt=final_consec and final_consec >=5;
quit;
``````

This will produce one record per np_resource_id/start_dt combination, but just those with a maximum consec_cnt>=5.

The program assumes for there are no "holes" in consec_cnt.  I.e. if there is a consec_cnt=6, there must also be a 1,2,3,4, and 5.

All Replies
Super User
Posts: 12,394

## Re: Sum based on date range

Your dates look like they are character variables. If so you likely want to create new variable that are SAS date valued numeric to use "range" of dates.

What specific range of dates do you need? You don't mention it. Also you picture of the data does not include a variable named concent_cnt  so saying anything about that variable isn't much help.

Contributor
Posts: 22

## Re: Sum based on date range

[ Edited ]

its actually consec_cnt (which is counting 5 consecutive days)

i do have a range of five days but when i sum the units it sums all the units but i want sum of units for specific dates

PROC SQL ;
CREATE TABLE NEW AS
SELECT NP_RESOURCE_ID, category, START_DT, ACTDATE As End_DT, CONSEC_CNT,
INTCK('day',START_DT, ACTDATE) as DAY_DIFFERENCE
FROM cosick1
WHERE CONSEC_CNT >= 5
ORDER BY NP_RESOURCE_ID, ACTDATE ;
QUIT

Solution
‎10-02-2017 05:39 PM
Posts: 1,229

## Re: Sum based on date range

Basically you need GROUP BY, not ORDER BY:

``````proc sql;
select np_resource_id,start_dt,activity_dt as end_dt,
sum(unit) as total_units,
max(consec_cnt) as final_consec,
end_dt-start_dt as difference
from have
group by np_resource_id,start_dt
having consec_cnt=final_consec and final_consec >=5;
quit;
``````

This will produce one record per np_resource_id/start_dt combination, but just those with a maximum consec_cnt>=5.

The program assumes for there are no "holes" in consec_cnt.  I.e. if there is a consec_cnt=6, there must also be a 1,2,3,4, and 5.

☑ This topic is solved.