Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

From |
Nick Cox <njcoxstata@gmail.com> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: st: Loop year variable |

Date |
Sun, 21 Oct 2012 23:23:21 +0100 |

See for technique here FAQ . . . . . . . . . . . . . . . . . . . . . . . Replacing missing values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox 2/03 How can I replace missing values with previous or following nonmissing values? http://www.stata.com/support/faqs/data/missing.html bysort iso_o iso_d (year) : gen datewon = year if !missing(FT_A, FT_B) & FT_A > FT_B by iso_o iso_d : replace datewon = datewon[_n-1] if missing(datewon) gen yrs_won = date - datewon Nick On Sun, Oct 21, 2012 at 10:37 PM, emanuele mazzini <madsoenistata@gmail.com> wrote: > Ok, let's ignore this additional issue for the moment. I can be > clearer I guess with the following example: > > +---------------------------------------------------------------------+ > | year iso_o iso_d team_A team_B FT_A FT_B date_won | > |---------------------------------------------------------------------| > 1. | 1982 ITA DEU Italy Germany FR 3 1 1982 | > 2. | 1983 ITA DEU . . . | > 3. | 1984 ITA DEU . . . | > 4. | 1985 ITA DEU . . . | > 5. | 1986 ITA DEU . . . | > |---------------------------------------------------------------------| > 6. | 1987 ITA DEU . . . | > 7. | 1988 ITA DEU . . . | > 8. | 1989 ITA DEU . . . | > 9. | 1990 ITA DEU . . . | > 10. | 1991 ITA DEU . . . | > |---------------------------------------------------------------------| > 11. | 1992 ITA DEU . . . | > 12. | 1993 ITA DEU . . . | > 13. | 1994 ITA DEU . . . | > 14. | 1995 ITA DEU . . . | > +---------------------------------------------------------------------+ > > As you can see, the variable date_won is =1982 only for the year in > which the match was played. Now, I would like to generate a new > variable that would tell me how many years have gone by since Italy's > last won (e.g, =0 for 1982, =1 for 1983, =2 for 1984, etc.). > As I said, I tried with: gen yrs_won=year-date_won but this is the result: > > +---------+ > | yrs_won | > |---------| > 1. | 0 | > 2. | . | > 3. | . | > 4. | . | > 5. | . | > |---------| > 6. | . | > 7. | . | > 8. | . | > 9. | . | > 10. | . | > |---------| > 11. | . | > 12. | . | > 13. | . | > 14. | . | > +---------+ > > As you can see, such command doesn't produce the expected result. That > explains why I was looking for a way to expand the value of date_won > for all cells. I thought that was the reason for the command not to > work. Do you have any suggestion for overcoming my problem? > > Thank you in advance, > > Regards, > > E.M. > > > > > 2012/10/21 Nick Cox <njcoxstata@gmail.com>: >> I gathered from your original posting that you had yearly data. I >> understand that matches may be more frequent than yearly, but I can't >> advise further without seeing what your data look like as the trade >> data and match data are not obviously compatible. >> >> Nick >> >> On Sun, Oct 21, 2012 at 1:00 PM, emanuele mazzini >> <madsoenistata@gmail.com> wrote: >>> Dear Mr Cox, >>> >>> thank you for your reply. The point, however, is that I cannot declare >>> my dataset to be time-series (how would like but I cannot) due to the >>> fact that some matches may take place once, twice or even more times >>> in the same year. That is why I was looking form a manual command. I >>> may better handle this first? And how can I do that in your opinion? 2012/10/21 Nick Cox <njcoxstata@gmail.com>: >>>> This should yield to a treatment in terms of spells in the sense of >>>> -tsspell- (SSC) and of >>>> >>>> SJ-7-2 dm0029 . . . . . . . . . . . . . . Speaking Stata: Identifying spells >>>> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox >>>> Q2/07 SJ 7(2):249--265 (no commands) >>>> shows how to handle spells with complete control over >>>> spell specification >>>> >>>> Look at -tsspell- first; it is self-contained. That is, your panels >>>> are defined by country pairs (dyads) and the start of a spell is >>>> defined by a match taking place (or a particular result; I don't >>>> follow exactly what you want, e.g what about draws?). Then a variable >>>> defined by -tsspell- gives you sequence in spell. You may need to >>>> subtract 1. >>>> >>>> I don't see that loops are needed. >>>> >>>> Nick >>>> >>>> On Sun, Oct 21, 2012 at 12:07 PM, emanuele mazzini >>>> <madsoenistata@gmail.com> wrote: >>>> >>>>> I am not a new Stata user, but I have to admit I still encounter some >>>>> problems with writing loops. A loop may not be the solution to the >>>>> issue I am going to describe, but I thought it is tough. >>>>> >>>>> I do have a dataset where I have country-pair trade observations for >>>>> years 1950-2006 as well as another information, which is football >>>>> match result for some dyads. I therefore need to generate a variable >>>>> that indicates how many years are passed since the last won/lost of a >>>>> country versus another. Suppose FT_A and FT_B are the final results >>>>> for team A and B. I thought to generate a variable as follows: >>>>> >>>>> gen int date_won=year if FT_A>FT_B | FT_A<FT_B >>>>> >>>>> in order to get a variable that is = year for every observation I have >>>>> information about, which reports the year of the won/loss. I then >>>>> thought to generate another variable (let's say yrs_won) to tell me >>>>> how many years have gone by as follows: >>>>> >>>>> gen int yrs_won=year-date_won >>>>> >>>>> Unfortunately, I do not get what I want: the only value yrs_won >>>>> assumes is 0, but for all years from 1983 until 2006 I would like it >>>>> to be 1, 2,3 and so on. >>>>> >>>>> At the beginning I didn't realize the problem, now it may be occur due >>>>> to the fact that date_won = year only for the year in which the match >>>>> was played. I guess I need to do something such as writing a loop to >>>>> let it spread over all cells of such variable, but I don't really know >>>>> how to make it. Does anyone of you has any suggestion? * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/faqs/resources/statalist-faq/ * http://www.ats.ucla.edu/stat/stata/

**Follow-Ups**:**Re: st: Loop year variable***From:*emanuele mazzini <madsoenistata@gmail.com>

**References**:**st: Loop year variable***From:*emanuele mazzini <madsoenistata@gmail.com>

**Re: st: Loop year variable***From:*Nick Cox <njcoxstata@gmail.com>

**Re: st: Loop year variable***From:*emanuele mazzini <madsoenistata@gmail.com>

**Re: st: Loop year variable***From:*Nick Cox <njcoxstata@gmail.com>

**Re: st: Loop year variable***From:*emanuele mazzini <madsoenistata@gmail.com>

- Prev by Date:
**Re: st: Loop year variable** - Next by Date:
**Re: st: -tabplot- updated on SSC** - Previous by thread:
**Re: st: Loop year variable** - Next by thread:
**Re: st: Loop year variable** - Index(es):