Hello everyone,

For my Replication Study, I am replicating a study for which I need the variable 'Return Volatility'. This is measured as the 'standard deviation of monthly stock returns over three years prior to t'. I am new to STATA, and I have no clue how to calculate the standard deviation over 3 preceding years. My data looks like this:

Cusip Permno Permco Companyname fyear month........date ......vwreturn
30710 .................................................. 2007 .....1......29/01/2007.......x ..........
30710 .................................................. 2007 .....2......29/02/2007.......x ..........
30710 .................................................. 2007..3/4/5 etc......x...............x ..........
30710 .................................................. 2007 .....12....29/12/2007.......x ..........
30710 .................................................. 2008 .....1......29/01/2008.......x ..........
30710 .................................................. 2007 .....2......29/02/2008.......x ..........

et cetera. I have data from 2007 till 2019, and thousands of unique companies.

From the monthly returns, I already calculated the annual returns (using by cusip fyear: egen yearendret = total(vwreturn). (Then using collapse function) Then my data looks like this:
Cusip Permno Permco Companyname fyear yearendret
30710 .................................................. 2007......x.........
30710 .................................................. 2008......x .........
30710 .................................................. 2009......x..........
30710 .................................................. 2010......x..........
30710 .................................................. 2011......x ..........


Now my question is, how do I calculate the SD for this yearendret?
I already tried: egen sd_yearendret= sd(yearendret), by(cusip fyear)
However, I only get values of 0.

In addition, how do I get the standard deviation of the three preceding years all together? Is it even useful to calculate the SD per year, or is there another way to calculate the SD of yearendret three years prior to t?

Kind regards,
Laura Banken