Bookmark and Share

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]

st: Looping 2x over same list


From   "Christopher O'Keefe" <[email protected]>
To   Stata list <[email protected]>
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.


Best,
Chris O'Keefe

Code:
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
        }
    }
}

clear
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
http://dss.ucsd.edu/~cokeefe

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


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