Hello everyone,
I have a dataset spanning from the year 2004 to 2021, comprising approximately 500,000 observations on average annually. The number of observations varies from year to year, and the number of observations for each cross-section (nuts2) also changes within the year.
I have average temperature and precipitation data within this dataset, and I would like to create their lags.
First, I attempted to use the following code, but it only subtracted the 1st observation of each year and did not generate the lags; it retained the same value:
sort nuts2 year
by nuts2: gen lag1temperature = avg_temp_nuts2[_n-1]
Then, I tried to create lag variables with the following code and received an error:
gen lag_avg_temp = L.avg_temp_nuts2
time variable not set r(111);
tsset year nuts2
repeated time values within panel r(451);
Could you please help me resolve this? I'm new to Statalist, so I apologize for doing anything incorrectly.
I have a dataset spanning from the year 2004 to 2021, comprising approximately 500,000 observations on average annually. The number of observations varies from year to year, and the number of observations for each cross-section (nuts2) also changes within the year.
I have average temperature and precipitation data within this dataset, and I would like to create their lags.
First, I attempted to use the following code, but it only subtracted the 1st observation of each year and did not generate the lags; it retained the same value:
sort nuts2 year
by nuts2: gen lag1temperature = avg_temp_nuts2[_n-1]
Then, I tried to create lag variables with the following code and received an error:
gen lag_avg_temp = L.avg_temp_nuts2
time variable not set r(111);
tsset year nuts2
repeated time values within panel r(451);
Could you please help me resolve this? I'm new to Statalist, so I apologize for doing anything incorrectly.
Comment