turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- Sum of data with sorted dates

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

10-02-2017 04:15 PM - edited 10-02-2017 05:43 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to hk2013

10-02-2017 05:23 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to hk2013

10-02-2017 04:33 PM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to ballardw

10-02-2017 04:38 PM - edited 10-02-2017 04:40 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to hk2013

10-02-2017 05:23 PM

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.