Bookmark and Share

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

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

st: Loop year variable

From   emanuele mazzini <>
Subject   st: Loop year variable
Date   Sun, 21 Oct 2012 13:07:11 +0200

Dear all,

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?

Thank you all in advance,


*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index