This question evoked a variety of correct
and incorrect answers. The most satisfying
solution from first principles is surely
bysort ccode (year) : gen nukecount = sum(nukes)
but with -tsset- data and -tsspell- from SSC
tsspell nukes
creates the variable required automatically
as -_seq-. The other variables created by -tsspell-
are probably no use here.
Nick
[email protected]
Michael
> I have a time series dataset organized by country year, with
> one entry per
> year per country. I want to create a count variable that measures the
> number of years it has been since a given country has acquired nuclear
> weapons, the variable of interest for my study. But I am
> having trouble
> getting the coding entirely right. The variables that I
> "think" matter
> here are:
>
> -ccode: the country code for each country
> -nukes: a binary variable that is 0 if a country does not have nuclear
> weapons and a 1 if it has nuclear weapons
> -year: this is pretty obvious. .
>
> The current coding I have is:
>
> gen nukecount=0
> replace nukecount=(nukecount[_n-1]+1) if ((ccode==ccode[_n-1]) &
> (nukes==1) & (nukes[_n-1]==1))
>
> If anyone has some advice about what I am doing wrong and how to
> potentialy correct it. . that would be fantastic and I would really
> appreciate it.
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/