Statalist The Stata Listserver


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

Re: st: RE: new local macro from results of loop


From   Ronnie Babigumira <rb.glists@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: RE: new local macro from results of loop
Date   Wed, 30 May 2007 18:43:03 +0200

My aha moment

Thank you both for the examples. Most illuminating

Ronnie

Maarten Buis wrote:
the logic is that if a local doesn't yet exists it will return nothing. How it works is best shown in an example:

*--------------- begin example -----------
di `foo'
local foo `foo'
di `foo'
local foo `foo' 1
di `foo'
local foo `foo' 2
di `foo'
*--------------- end example --------------
(For more on how to use examples I sent to the Statalist, see
http://home.fsw.vu.nl/m.buis/stata/exampleFAQ.html )

Hope this helps,
Maarten

-----------------------------------------
Maarten L. Buis
Department of Social Research Methodology Vrije Universiteit Amsterdam Boelelaan 1081 1081 HV Amsterdam The Netherlands

visiting address:
Buitenveldertselaan 3 (Metropolitan), room Z434
+31 20 5986715

http://home.fsw.vu.nl/m.buis/
-----------------------------------------

-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu]On Behalf Of Ronnie Babigumira
Sent: woensdag 30 mei 2007 17:46
To: statalist@hsphsun2.harvard.edu
Subject: Re: st: RE: new local macro from results of loop

Thanks Nick and Maarten

Both of your suggestions work (they look the same save for the "" in Maartens solution). That said, I have missed the
obvious so I will ask that you help me with the logic

My part of the loop goes through each element and does my stuff

foreach v of local lvstk {
--my stuff--

How exactly does your part work
local lvstknos "`lvstknos' `v'nbeg `v'nend"
local new `new' `v'nbeg `v'nend

It appears that you are each amending a new local (part of my confusion is that this new local has not been initialised
before). My other problem is that I don't see the part that instructs Stata to incrementally build the new local

Again, your solutions both work . I just want to understand the logic

Many thanks

Ronnie

Maarten Buis wrote:

foreach v of local lvstk {
    egen `v'nbeg = sum(liv_begnum) if `v', by(houscode qtr)
    egen `v'nend = sum(liv_endnum) if `v', by(houscode qtr)
    local lvstknos "`lvstknos' `v'nbeg `v'nend"
}

Hope this helps,
Maarten

NB notice the absence of the equal sign in the local
statement, this will protect you against strings that
are too long.

-----------------------------------------
Maarten L. Buis
Department of Social Research Methodology
Vrije Universiteit Amsterdam
Boelelaan 1081
1081 HV Amsterdam
The Netherlands

visiting address:
Buitenveldertselaan 3 (Metropolitan), room Z434

+31 20 5986715

http://home.fsw.vu.nl/m.buis/
-----------------------------------------

-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu]On Behalf Of Ronnie Babigumira
Sent: woensdag 30 mei 2007 16:35
To: statalist@hsphsun2.harvard.edu
Subject: st: new local macro from results of loop

Dear list

I define a local macro lvstk below which contains variables I would like to process further. For example

local lvstk "cattle largeanims calves smallrums smallanims"

foreach v of local lvstk {
        egen `v'nbeg = sum(liv_begnum) if `v', by(houscode qtr)
        egen `v'nend = sum(liv_endnum) if `v', by(houscode qtr)
}

how do I automatically put the new variables being generated into a new macro

say

local lvstknos "cattlenbeg cattlenend ....and-all-the-other-stuff-coming-out-of-the-foreach-loop"

Ronnie
*
*   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/

*
*   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/

*
*   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/

*
*   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/

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index