DATA Step, Macro, Functions and more

how rolling standard deviation facing two consecutive 0 value restart counting

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 12
Accepted Solution

how rolling standard deviation facing two consecutive 0 value restart counting

Hello all,

 

The data have is as follows. I expect the result to show the rolling 3years std by  treatment, if the treatment facing one 0 value will continuted to count, but if the treatment facing two consecutive 0 value will be restarted to count. Now assuming 2 treatments, could you do me a great favor to get the result as follows? (the numerical1 to 6 just represents 6 different numerical value for rolling 3 years standard deviations). Thanks a lot.

 

data have;
input treatment year x;
cards;

treatment   year   x     

1              1980     0      
1              1981     0      
1              1982     0      
1              1983     0    
1              1984     5      
1              1985      7     
1              1986      0     
1              1987      6    
1             1988       2    
1             1989      1     
2             1982      0         
2             1983       0       
2            1984       3     
2            1985        0      
2           1986        0       
2           1987        3       
2            1988       0         
2           1989       1         
2           1990       1        


;
run;

 

I hope the result like this:

treatment   year   x      count   std3yr

1              1980     0      0            .
1              1981     0      0            .
1              1982     0      0            .     
1              1983     0      0            .
1              1984     5      1            .
1              1985      7     2            .
1              1986      0     3     numerical1
1              1987      6    4      numerical2
1             1988       2    5      numerical3
1             1989      1     6      numerical4
2             1982      0     0       .    
2             1983      0     0       .
2             1984      3     1       .
2            1985       0      2      .
2            1986       0      0      .
2            1987       3      1      .
2            1988       0      2      . 
2            1989      1       3      numerical5
2            1990      1       4      numerical6

 

 


Accepted Solutions
Solution
‎01-06-2017 08:53 AM
PROC Star
Posts: 7,487

Re: how rolling standard deviation facing two consecutive 0 value restart counting

Posted in reply to Jennifer925

I think that the following does what you are looking for:

 

data want (drop=_Smiley Happy;
set have;
by treatment;
_x1=lag1(x);
_x2=lag2(x);
if first.treatment then do;
count=0;
call missing(_x1);
call missing(_x2);
end;
if x gt 0 or (x eq 0 and _x1 gt 0) then count+1;
else count=0;
if count ge 3 then std3yr=sum(_x1, _x2, x)/3;
run;

 

HTH,

Art, CEO, AnalystFinder.com

View solution in original post


All Replies
Solution
‎01-06-2017 08:53 AM
PROC Star
Posts: 7,487

Re: how rolling standard deviation facing two consecutive 0 value restart counting

Posted in reply to Jennifer925

I think that the following does what you are looking for:

 

data want (drop=_Smiley Happy;
set have;
by treatment;
_x1=lag1(x);
_x2=lag2(x);
if first.treatment then do;
count=0;
call missing(_x1);
call missing(_x2);
end;
if x gt 0 or (x eq 0 and _x1 gt 0) then count+1;
else count=0;
if count ge 3 then std3yr=sum(_x1, _x2, x)/3;
run;

 

HTH,

Art, CEO, AnalystFinder.com

SAS Super FREQ
Posts: 3,755

Re: how rolling standard deviation facing two consecutive 0 value restart counting

std3yr=sum(_x1, _x2, x)/3;

is presumably a typo. I think Art meant

std3yr=std(_x1, _x2, x);

Occasional Contributor
Posts: 12

Re: how rolling standard deviation facing two consecutive 0 value restart counting

@Rick_SAS

Thank you very much.
PROC Star
Posts: 7,487

Re: how rolling standard deviation facing two consecutive 0 value restart counting

Rick,

 

Thanks for correcting my mental typo.

 

Art, CEO, AnalystFinder.com

 

p.s. Happy New Year!

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 172 views
  • 0 likes
  • 3 in conversation