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!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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