Hi,
based on Ksharp's answer in https://communities.sas.com/t5/SAS-Procedures/rolling-standard-deviation-calculation/m-p/160027#M41709 I want to calculate the rolling standard deviation but only for certain observations - the ones that have flag=1:
data have;
infile cards dlm=',' truncover;
input code year x flag;
cards;
255956,1980,,
255956,1981,,
255956,1982,,
255956,1983,,
255956,1984,5,
255956,1985,7,
255956,1986,4,
255956,1987,6,1
255956,1988,2,
255956,1989,1,
255964,1980,5,
255964,1981,7,
255964,1982,,
255964,1983,,
255964,1984,5,
255964,1985,3,
255964,1986,7,
255964,1987,3,
255964,1988,8,1
255964,1989,7,
;
run;
When I did the following code (also based on Ksharp's code):
proc sql;
create table want as
select *,(select std(x) from have where year between a.year-2 and a.year and code=a.code) as rolling_std
from have as a
where flag =1;
quit;
I obtained a data set containing only 2 observations. Indeed these are the observations where flag=1, but I would like to keep the original data as well. Also, I assume that here the rolling standard deviation was calculated for all observations where possible, but since my data is very big, it might be helpful to calculate the rolling standard deviation ONLY for the observations that have flag=1.
Thank you!
... View more