## Add a cumulative count that resets when value = 0

Solved
Occasional Contributor
Posts: 8

# Add a cumulative count that resets when value = 0

Hi, this is probably an easy question, am looking to add a cumulative  # of days a student made a mistake, and this resets to 0 when the student stop making mistakes. I have created a simple table to further illustrate my needs.

 Date Mistake_Flg Days_Mistake_Streak 1-Jan 0 0 2-Jan 0 0 3-Jan 1 1 4-Jan 1 2 5-Jan 1 3 6-Jan 0 0 7-Jan 0 0 8-Jan 1 1 9-Jan 1 2 10-Jan 0 0

Where "Days_mistake_streak" is what I'm trying to derive.

Thanks!

Accepted Solutions
Solution
‎10-23-2013 11:39 AM
PROC Star
Posts: 8,167

## Re: Add a cumulative count that resets when value = 0

You could use something like:

data want;

set have;

retain Days_Mistake_Streak;

Days_Mistake_Streak=ifn(Mistake_Flg eq 0,0,Days_Mistake_Streak+1);

run;

All Replies
Solution
‎10-23-2013 11:39 AM
PROC Star
Posts: 8,167

## Re: Add a cumulative count that resets when value = 0

You could use something like:

data want;

set have;

retain Days_Mistake_Streak;

Days_Mistake_Streak=ifn(Mistake_Flg eq 0,0,Days_Mistake_Streak+1);

run;

Occasional Contributor
Posts: 8

## Re: Add a cumulative count that resets when value = 0

worked like a charm, thanks so much Arthur!

🔒 This topic is solved and locked.

Discussion stats
• 2 replies
• 198 views
• 1 like
• 2 in conversation