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
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
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
std3yr=sum(_x1, _x2, x)/3;
is presumably a typo. I think Art meant
std3yr=std(_x1, _x2, x);
Rick,
Thanks for correcting my mental typo.
Art, CEO, AnalystFinder.com
p.s. Happy New Year!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.