Hi,
I want to create a variable which is defined as the standard deviation of population over the last 3 years (prior to the current year) for each country. Furthermore, there have to be only 2 observations for the population within the last three years for calculating the standard deviation. (I.e. if there is one missing value for population within the last 3 years, the standard deviation should be calculated as the standard deviation of the 2 years within the 3 years period, where a number for population was observed. If there are observations for all three years, the standard deviation should be calculated from those 3 values. If there is only one observation during the 3 year period, a missing value should be generated for the variable standard deviation.)
In my attached example:
The standard deviation of country 1 in year 1973 should be calculated as the standard deviation of the values for population in 1972 and 1971 (as only 2 observations are required and the value for 1970 is missing).
The standard deviation of country 1 in year 1974 should be calculated as the standard deviation of the values for population in 1973, 1972 and 1971.
I set the data to be Panel by: xtset country year
I know that there is the command: egen standard_deviation = sd(expression) and that there is the option by
Unfortunately, I don't know how to use this command for my problem.
Does anyone have an idea how to solve my problem?
Thanks.
I want to create a variable which is defined as the standard deviation of population over the last 3 years (prior to the current year) for each country. Furthermore, there have to be only 2 observations for the population within the last three years for calculating the standard deviation. (I.e. if there is one missing value for population within the last 3 years, the standard deviation should be calculated as the standard deviation of the 2 years within the 3 years period, where a number for population was observed. If there are observations for all three years, the standard deviation should be calculated from those 3 values. If there is only one observation during the 3 year period, a missing value should be generated for the variable standard deviation.)
In my attached example:
The standard deviation of country 1 in year 1973 should be calculated as the standard deviation of the values for population in 1972 and 1971 (as only 2 observations are required and the value for 1970 is missing).
The standard deviation of country 1 in year 1974 should be calculated as the standard deviation of the values for population in 1973, 1972 and 1971.
I set the data to be Panel by: xtset country year
I know that there is the command: egen standard_deviation = sd(expression) and that there is the option by
Unfortunately, I don't know how to use this command for my problem.
Does anyone have an idea how to solve my problem?
Thanks.
Comment