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: re: loops - beginner's question

From   Christopher Baum <>
To   <>
Subject   st: re: loops - beginner's question
Date   Mon, 1 Nov 2010 09:36:13 -0400

Maik said

I have a panel dataset of firm directors. The dataset is a result of
two sets of data. The problem is, that one dataset uses a different
director identifier than the second. However, some some overlap does
exist. I would like to create a loop which replaces the missing
identifier with the once used in the year before. The loop should
check if the idendifier is missing and if so check if the firm
identifier is identical to last year. I was usind the following code:

sort firm_id year
gen temp_id=dir_id if dir_id!=.
by cusip: replace temp_id=dir_id[_n-1] if firm_id==firm_id[_n-1] &

The code only replace the first observation but I have more than one
directors in each year. I have experimenting with the following code,
which obviously does not do the job.

Can somebody please help or direct me a bit?

while condition on `local’ true {
local i=1
while `i' =. {
 if firm_id=firm_id in year=year-1
replace `i'= dir_id in year=year-1
local i=`i'+1

First of all, in this latter fragment, the tests for equality in your while, if and replace statements must be ==, not =.

I find your initial block of code confusing. Is firm_id identifying directors, or firms? If the latter, why is 
there also a cusip (presumably identifying firms)? Presumably for each firm (cusip) you have multiple
directors, and for each director you have, potentially, multiple years. Thus it would seem that
the first block of code should sort by -cusip firm_id year- and the by prefix should be -by cusip firm_id-. 
Then, if there are no missing years, you should be able to update temp_id by merely saying, within
that -by-, 

replace temp_id = firm_id[_n-1] if mi(firm_id) & _n > 1

assuming that the firm_id is defined in the first year for which that firm|director combination exists, that
will propagate the firm_id forward in time.


Kit Baum   |   Boston College Economics & DIW Berlin   |
                              An Introduction to Stata Programming  |
   An Introduction to Modern Econometrics Using Stata  |

*   For searches and help try:

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