Notice: On March 31, it was **announced** that Statalist is moving from an email list to a **forum**. The old list will shut down on April 23, and its replacement, **statalist.org** is already up and running.

[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 12:17:31 +0100 |

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>

- Prev by Date:
**st: Loop year variable** - Next by Date:
**st: Identifying Experiment subjects that belong in both the Placebo and Experiment Groups** - Previous by thread:
**st: Loop year variable** - Next by thread:
**Re: st: Loop year variable** - Index(es):