Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: loop over pair of elements


From   Caleb Southworth <[email protected]>
To   [email protected]
Subject   st: loop over pair of elements
Date   Sat, 16 Dec 2006 12:11:50 -0800 (PST)

I have a set of pairs of observations that I would like to combine.
The new variables will be sums. This is useful, for example, when
merging historical census tracts which have been divided into two
tracts at a later time period. There are about 30 pairs of such
polygons and the operation has to be performed several times.

One solution is to identify the two observations to be merged and then
repeat this bit of code for each pair saving a temp file. For example:

local c1=593
local c2=759
keep if id==`c1'|id==`c2'
qui ds w_1888-_2004
collapse (sum) "`r(varlist)'"
gen id=`c1'
save temp1, replace

I would prefer to do this with a loop where the -foreach- command read
two strings at a time, so

forval c1 c2 = 593 759 588 132 {
	keep if id==`c1'|id==`c2'
	qui ds w_1888-_2004
	collapse (sum) "`r(varlist)'"
	gen id=`c1'
}

-forval- does not appear to work that way.

Caleb
[email protected]
University of Oregon Sociology
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index