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 at the end of May, and its replacement, statalist.org is already up and running.


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

Re: st: Looping with merge


From   Nick Cox <njcoxstata@gmail.com>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
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
njcoxstata@gmail.com


On 13 May 2013 15:49, Nick Cox <njcoxstata@gmail.com> 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
> njcoxstata@gmail.com
>
>
> On 13 May 2013 15:42, Adam Guerrero <adamgue@gmail.com> 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/


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