BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Jennifer925
Fluorite | Level 6

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

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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

 

data want (drop=_:);
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

4 REPLIES 4
art297
Opal | Level 21

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

 

data want (drop=_:);
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

Rick_SAS
SAS Super FREQ

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

is presumably a typo. I think Art meant

std3yr=std(_x1, _x2, x);

art297
Opal | Level 21

Rick,

 

Thanks for correcting my mental typo.

 

Art, CEO, AnalystFinder.com

 

p.s. Happy New Year!

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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