# Cummulative Number

#### Janoose

##### New Member
Hi I am having trouble with cummulating by group.

I tried to get the samples online but I kept getting an error message saying "By variable not sorted properly" when I did sort but I suspect not the second variable.

The table is basically list of patients with their bed location transfer. In the same nursing unit the patient can switch beds. I am trying to get their total length of stay at each nursing units during the visit. The purpose is to filter out patients who stayed less than 48 hours if the first nursing unit after they arrived to the hospital was "A".

Here is the sample of the table with DayStayed is the result that I want to achieve.

ID Admission Date CheckinDateTime CheckoutDatetime NursingUnit DayStayed
1111 Jan 1, 2014 8:00AM Jan 1, 2014 8:00AM Jan 2, 8:00 AM A 1
1111 Jan 1, 2014 8:00AM Jan 2, 2014 8:00AM Jan 3, 2014 8:00AM A 2

Here is the code:
Code:
proc sort data=noSignoff2;
by ID CheckinDateTime   ;
run;

data noSignoff2A;
set noSignoff2;
by ID nursingunit;
if first.ID and first.nursingunit then DayStayed=0;
DayStayed+ LOSInUnit;
*Note: LOSInUnit is a field I created to calculated days difference between checkindatetime and checkoutdatetime;
if last.ID and last.nursingunit then output;
drop DayStayed;
run;