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

dear all 

i have to compute rolling standard deviation based on past five years values of a variable. 

my data set is as follows 

 

Company_Nameyearsalesstd. dev
20 Microns Ltd.2005565.2 
20 Microns Ltd.2006668.9 
20 Microns Ltd.2007869.1 
20 Microns Ltd.20081067.8 
20 Microns Ltd.20091374.9=STDEV(C2:C6)
20 Microns Ltd.20101768.2=STDEV(C3:C7)
3I Infotech Ltd.20052064.7 
3I Infotech Ltd.20062755.5 
3I Infotech Ltd.20073313.1 
3I Infotech Ltd.20084451 
3I Infotech Ltd.20095249.6=STDEV(C8:C12)
3I Infotech Ltd.20105195.3=STDEV(C9:C13)

 

it is in panel data format with more than 3000 companies from 2005 to 2018. 

the calculation of standard deviation should be done company-wise. 

i want to give equal weightage to all the observation for a company.

there are no missing values in the dataset. 

 

thanks in advance 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

I like to use PROC EXPAND for problems like this. However, if you do not have an ETS license, you can use PROC SQL and do something like this

 

data have;
input Company_Name :$50. year sales;
infile datalines dlm=',';
datalines;
20 Microns Ltd.,2005,565.2
20 Microns Ltd.,2006,668.9
20 Microns Ltd.,2007,869.1
20 Microns Ltd.,2008,1067.8
20 Microns Ltd.,2009,1374.9
20 Microns Ltd.,2010,1768.2
3I Infotech Ltd.,2005,2064.7
3I Infotech Ltd.,2006,2755.5
3I Infotech Ltd.,2007,3313.1
3I Infotech Ltd.,2008,4451
3I Infotech Ltd.,2009,5249.6
3I Infotech Ltd.,2010,5195.3
;

proc sql;
   create table want as
   select *, 
          (select std(sales) from have 
             where Company_Name=a.Company_Name
               and a.year-5 le year le a.year)
                 as stddev format=8.2
   from have as a;
quit;

View solution in original post

2 REPLIES 2
PeterClemmensen
Tourmaline | Level 20

Do you have a SAS/ETS license? 

PeterClemmensen
Tourmaline | Level 20

I like to use PROC EXPAND for problems like this. However, if you do not have an ETS license, you can use PROC SQL and do something like this

 

data have;
input Company_Name :$50. year sales;
infile datalines dlm=',';
datalines;
20 Microns Ltd.,2005,565.2
20 Microns Ltd.,2006,668.9
20 Microns Ltd.,2007,869.1
20 Microns Ltd.,2008,1067.8
20 Microns Ltd.,2009,1374.9
20 Microns Ltd.,2010,1768.2
3I Infotech Ltd.,2005,2064.7
3I Infotech Ltd.,2006,2755.5
3I Infotech Ltd.,2007,3313.1
3I Infotech Ltd.,2008,4451
3I Infotech Ltd.,2009,5249.6
3I Infotech Ltd.,2010,5195.3
;

proc sql;
   create table want as
   select *, 
          (select std(sales) from have 
             where Company_Name=a.Company_Name
               and a.year-5 le year le a.year)
                 as stddev format=8.2
   from have as a;
quit;

SAS Innovate 2025: Call for Content

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!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 2441 views
  • 2 likes
  • 2 in conversation