Bookmark and Share

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, is already up and running.

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

st: Looping 2x over same list

From   "Christopher O'Keefe" <>
To   Stata list <>
Subject   st: Looping 2x over same list
Date   Tue, 30 Mar 2010 15:15:48 -0700

Hi all-
I'm attempting to create a dataset that assesses overlap in recipients
across multilateral aid institutions (donors).  I have created
separate datasets of recipients (by institution) and now I want to
compare overlap across pairs of institutions.  So far, I have
attempted to do this by looping over the values of a macro called
donor, once for each part of the dyad.  Of course, this results in
dyads x-y, y-x, and x-x.  I want to figure out how to get rid of the
y-x dyads (and x-x dyads too, but those are easy enough to get rid of
without losing important data).

In some respects this is similar to the problems here and here (in
section 3).  What is different is that I'm looping over the same list
to merge files, create variables, save files, and append everything
into the final dataset.  Does anyone have any insight into how I can
stop producing the y-x dyads (or how to exclude them when I append
these files into a single dataset?). (Code is below). Thanks so much
for your help.

Chris O'Keefe

foreach d of local donor {
    foreach e of local donor {
        foreach p of local ten_yr {
            use `d'_ten`p'_comp.dta, clear
            merge recipient using `e'_ten`p'_comp.dta
            gen shar_recip = 1 if _merge == 3
            replace shar_recip = 0 if _merge != 3
            collapse (sum) shar_recip
            local n = r(N)
            gen shar_recip_b = shar_recip/`n'
            *gen shar_recip_10 = shar_recip/10
            gen dyad = "`d'-`e'"
            save `d'_`e'_ten`p', replace

tempfile base
gen shar_recip = .
gen dyad = ""
save `base'

use `base', clear
foreach d of local donor {
    foreach e of local donor {
        foreach p of local ten_yr {
            capture append using `d'_`e'_ten`p'
            drop if dyad == "`d'-`d'"

PhD Student
University of California, San Diego
Dept. of Political Science

*   For searches and help try:

© Copyright 1996–2016 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index