Dear all,
I am facing some issues and I don't find any topic on my subject (sorry in advance if it has already been answered). Here is what I want to do: I want to create a variable that sums the first occurrence of the "object" variable within each region year group for the last three years. Let's call this variable sum3Y. My dataset looks like this:
region year object
"Dushanbe" 2008 0
"Dushanbe" 2008 0
"Dushanbe" 2008 0
"Dushanbe" 2008 0
"Dushanbe" 2008 0
"Dushanbe" 2008 0
"Dushanbe" 2008 0
"Dushanbe" 2008 0
"Dushanbe" 2008 0
"Dushanbe" 2008 0
"Dushanbe" 2008 0
"Dushanbe" 2008 0
"Dushanbe" 2008 0
"Dushanbe" 2009 1
"Dushanbe" 2009 1
"Dushanbe" 2009 1
"Dushanbe" 2009 1
"Dushanbe" 2009 1
"Dushanbe" 2009 1
"Dushanbe" 2009 1
"Dushanbe" 2009 1
"Dushanbe" 2009 1
"Dushanbe" 2009 1
"Dushanbe" 2009 1
"Dushanbe" 2009 1
"Dushanbe" 2009 1
"Dushanbe" 2009 1
"Dushanbe" 2009 1
"Dushanbe" 2009 1
"Dushanbe" 2009 1
"Dushanbe" 2009 1
"Dushanbe" 2009 1
"Dushanbe" 2010 1
"Dushanbe" 2011 0
"Dushanbe" 2012 0
"Dushanbe" 2013 0
"Dushanbe" 2013 0
"Dushanbe" 2013 0
"Dushanbe" 2014 0
For instance, sum3Y for the year 2011 should be equal to 2: first occurrence in Dushanbe 2011 = 0 + first occurrence in Dushanbe 2010 = 1 + first occurrence in Dushanbe 2009 = 1 + first occurrence in Dushanbe 2008 = 0 = 2. In the same logic, sum3Y for the year 2013 should be equal to 1.
My main problem so far has been that I do not have the same number of observations per year. I tried this command:
by region: gen CP3Y = object + object[_n-1] + object[_n-2] + object[_n-3]
But it did not give me the expected outcome.
Many thanks in advance
Related Posts with Sum of the three last years first osbervation within groups
comparing two timelines and converting date from 1960 to 2020I have multiple timelines I am attempting to analyse. After converting the dates to a numerical form…
Attempting to export two-way cross-tabulations using tab2xl - having issues in exporting it in the format I need it to be.* I am pasting my do-file here using a common data set. ***data for question webuse auto codebook f…
Meta regression: Random and Fixed effects / reg and aregDear all, I have a question that is probably specific to meta regression analyses. I refer to Feld…
Newey-West regressionHi, I am trying to run a Newey-West regression, however I do not quite understand how should I choos…
How to get concise regression tables from Stata?I wanted to know what is the best way to export regression results table in the most concise/smalles…
Subscribe to:
Post Comments (Atom)
0 Response to Sum of the three last years first osbervation within groups
Post a Comment