Hello all,

I am hoping for a bit of help with the generation of a new variable. I have some experience with STATA, but I am still very new to the software.

Although my data contains many variables for this question only three variables are important. Firm, Year, and Score. I am wanting to create a new variable that is the Standard Deviation of the Score variable that lagged by one year and includes the past 3 years scores to compute the standard deviation. This calculation needs to be by firm. I know I may not be communicating this clearly, so I am including some sample data to illustrate my goal.
Firm Year Score
A 1991 90
A 1991 95
A 1992 96
A 1992 92
A 1993 93
A 1993 94
A 1994 96
A 1994 93
A 1995 95
A 1995 94
A 1996 94
A 1996 87
A 1996 93
A 1997 94
A 1998 90
A 1999 90
A 1999 97
A 1999 91
A 2000 92
A 2001 92
A 2001 87
A 2002 94
B 1995 89
This is a small sample of my data and includes the first firm in the data set. Let's call the new variable SDScore. For the row A 2002 94, the value of SDScore should be the standard deviation of the values of Score in the Year 2001 2000 and 1999. Similar logic for the other years until the year 1993. For the row A 1993 94 and the row A 1993 93 the value of SDScore should be the standard deviation of the Score values in the Year 1992 and 1991. Finally for 1992 and for 1991 SDScore should be calculated using only the 1991 values for Score.

Finally, it is important to note that not every firm's observations are in the same year. For instance, firm B has observations that go back to 1963.

Are there any commands I can use to calculate SDScore in my dataset? Thanks!

Best,
Daniel