Hi everybody! This is my first post here. Hope I am making sense of myself.

I have a hospital activity dataset showing 2 (there are more in the real data) indicators for each hospital and month. I need to prepare the data for producing a run chart. I need to highlight in the run chart if there is a shift, i.e. 6 or more consecutive points are either all above or all below the median value. However, values that fall on the median do not add to nor break a shift. I need to skip values that fall on the median and continue counting.

Month Hospital Indicator Rate Median Difference
1 A111H LengthofStay 12.79 15.158 <0
2 A111H LengthofStay 16.48 15.158 >0
3 A111H LengthofStay 11.7 15.158 <0
4 A111H LengthofStay 17.66 15.158 >0
5 A111H LengthofStay 15.16 15.158 =0
6 A111H LengthofStay 18.77 15.158 >0
7 A111H LengthofStay 11.7 15.158 <0
1 A111H Mortality 34.67 24.324 <0
2 A111H Mortality 23.08 24.324 <0
3 A111H Mortality 36.19 24.324 >0
4 A111H Mortality 36.04 24.324 >0
5 A111H Mortality 29.13 24.324 >0
6 A111H Mortality 30.5 24.324 >0
7 A111H Mortality 29.69 24.324 >0
8 A111H Mortality 29.03 24.324 <0


I created a new variables called Difference to show the difference between the rate and median: >0, <0 or =0. I have managed to use the lag function and lots of ‘ if’ statements to flag 6 or more consecutive points above/below the median for each hospital and indicator. However, I cannot think of a way to tell it to skip values that fall on the median and continue counting. Maybe it is some sort of loop I am looking for?

Thanks very much for your help!

Echo