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]
Re: st: Looping with merge
From
Nick Cox <[email protected]>
To
"[email protected]" <[email protected]>
Subject
Re: st: Looping with merge
Date
Mon, 13 May 2013 16:28:28 +0100
For a recent thread centred on the same problem -- a single loop over
two or more lists being wrongly written as a multiple loop -- start
with
http://www.stata.com/statalist/archive/2013-03/msg00983.html
including the reference cited therein.
Nick
[email protected]
On 13 May 2013 15:49, Nick Cox <[email protected]> wrote:
> It's one loop, not two.
>
> local files zip_pov p15_zip p37_zip
> local vars povrate permcf perbachelor
>
> forval i = 1/3 {
> local f : word `i' of `files'
> local v : word `i' of `vars'
> merge m:1 zip_num using `f', keepusing(`v')
> drop if _merge==2
> drop _merge
> }
>
> OR
>
> forval i = 1/3 {
> local f : word `i' of zip_pov p15_zip p37_zip
> local v : word `i' of povrate permcf perbachelor
> merge m:1 zip_num using `f', keepusing(`v')
> drop if _merge==2
> drop _merge
> }
>
> Nick
> [email protected]
>
>
> On 13 May 2013 15:42, Adam Guerrero <[email protected]> wrote:
>> Hello All,
>>
>> I am trying to write a loop for the following lines of code
>>
>> merge m:1 zip_num using zip_pov.dta, keepusing(povrate)
>> drop if _merge==2
>> drop _merge
>> merge m:1 zip_num using p15_zip.dta, keepusing(permcf)
>> drop if _merge==2
>> drop _merge
>> merge m:1 zip_num using p37_zip.dta, keepusing(perbachelor)
>> drop if _merge==2
>> drop _merge
>>
>> This is what, but has not been working
>>
>> foreach file in zip_pov.dta p15_zip.dta p37_zip.dta {
>> foreach var of varlist "povrate" "permcf" "perbachelor" {
>> merge m:1 zip_num using `file', keepusing(`var')
>> drop if _merge==2
>> drop _merge
>> }
>> }
>>
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/faqs/resources/statalist-faq/
* http://www.ats.ucla.edu/stat/stata/