Hi all,
I'm creating my own moving average code since I only want the top four of the previous seven years average for my variable max_yield. I'm getting the error in the forvalues loop. I included the rest in case it can help provide context.
Thanks in advance.
sort nrate_year // sort the data by year before running "by nrate_year"
by nrate_year, sort: egen max_yield = max(cornyieldbuac)
egen j = seq(), f(1) t(47) //generates repeating sequence to isolate each year's max yield
xtset j nrate_year
sort j max_yield
forvalues y = 2016/2020 {
// generate a variable that is equal to 1 for the years that are within the previous seven years of the current year
gen in_range = (nrate_year >= `y'-7 & nrate_year <= `y'-1)
// tag the top four observations in each group
by j: egen top_four = tag(max_yield, tag(1 2 3 4) group)
// generate the moving average of the best four years for the current year
by j: egen ma = mean(max_yield) if nrate_year == `y' & top_four == 1
// drop the intermediate variables
drop in_range top_four
}
Related Posts with Moving average invalid syntax
How do I use XTSET in panel data?Hello, I want to assess the impact of migration on the trade. So I have trade data between one dest…
Initial condition y_i,0 in a dynamic model with individual specific effects (following Chamberlain's approach)Dear all, I was wondering how the initial observation y_i,0 should look like in the following AR(1)…
confused about restructuring data: reshaping? stacking? something else?Hi, I am using Stata 16.1 on Windows 10. I have attached two datasets: (1) a simple example of the s…
Why we need to use fixed effect for binary variable?When trying to explain the interaction variable explanation, I saw this link. In this link, I saw a …
How to replace missing day/month with a "generic" date (i.e. 01jan) for a string date variableHello, I am working with a string variable called "YearofBirth" that includes includes 443 observati…
Subscribe to:
Post Comments (Atom)
0 Response to Moving average invalid syntax
Post a Comment