- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data temp;
input group x;
cards;
1 23
1 34
1 5
1 15
2 78
2 92
2 45
2 89
2 34
2 76
3 31
4 23
4 12
;
run;
data cusum;
set temp;
by group;
if first.group then total=0;
total+x;
if total >= 35 then flag='Y';
run;
How to reset the total equal to x once the threshold is reached.
In the above example,id 1 reaches threshold (>=35) after second iteration(23+34).
so I would want the flag='Y at that record only and then for third iteration total must be equal to x and then cumulative addition
must be performed. the thrid and foruth iteration values of total must 5 and 20
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Depending upon what you actually want, you may only have to add one extra condition to your if statement:
data cusum;
set temp;
by group;
if first.group or total >= 35 then total=0;
total+x;
if total >= 35 then flag='Y';
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hiya, try:
data cusum;
set temp;
by group;
if first.group then total=0;
total+x;
if total >= 35 then do ;
flag='Y';
total = 0 ;
end ;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
It is setting total to 0 at the threshold.but I want the total to be actual total and the next observation should be equal to value of x
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Try:
data cusum (drop = A B) ;
set temp;
by group;
if first.group then A=0;
A+x;
if A >= 35 then do ;
flag='Y';
B = A ;
A = 0 ;
end ;
total = sum(A,B) ;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanks Steve.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanks Art. I tried and works.