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]

Re: st: How to consecutively add elements to a macro


From   Keith Dear <[email protected]>
To   [email protected]
Subject   Re: st: How to consecutively add elements to a macro
Date   Thu, 3 Mar 2011 11:54:27 +1100

Thomas,
You do it like this:
   local readyvar `readyvar' gender
If local readyvar currently holds "birthday", this command is expanded to
   local readyvar birthday gender
which I take it is what you need; and then next time
   local readyvar `readyvar' smoking
is expanded to
   local readyvar birthday gender smoking
and so on. And yes you are right about the evanescence of locals vs
globals. You can use the same idea with globals:
   glo readyvar $readyvar gender
Keith

On 3 March 2011 08:53, Thomas Speidel <[email protected]> wrote:
>
> On Wed, 02 Mar 2011 14:39:36 -0700, Thomas Speidel <[email protected]> wrote:
>>
>> I am working with multiple datasets where I have to modify and
>> extract variables to be assembled into a new dataset.  To keep track
>> of things in my do file, I would like to create a macro, say readyvar,
>> which stores the variable names as they become ready:
>>
>> ***********EXAMPLE**************
>> local readyvar
>> use data1.dta
>> <do something>
>> save temp1
>> local readyvar birthday
>>
>> use data2.dta
>> <do something>
>> save temp2
>> local readyvar <want to _add_ gender to the macro>
>>
>> use data3.dta
>> <do something>
>> save temp3
>> local readyvar <want to _add_ smoking to the macro>
>>
>> <...>
>> use temp1
>> merge id using temp2 temp3, sort
>> keep readyvar
>> **********END EXAMPLE************
>>
>> My question is how to I add elements to an existing macro without
>> having to re-type the whole content of the macro each time?  Also, if
>> I understand correctly, local macros exist only if I run the whole do
>> file at once.  In other words, if I run 'chunks' of the do files one
>> at a time, I would not be able to use the macro that was created in
>> the previous 'chunk'.  Is this one of those cases where a global macro
>> is preferrable?
>>
>> Thank you.
>
> --
> Thomas Speidel
> *
> *   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/



--
Dr Keith Dear
National Centre for Epidemiology and Population Health
ANU College of Medicine, Biology and Environment
Australian National University
Canberra, ACT 0200 Australia
CRICOS provider #00120C
Phone +61 (02) 6273 2208
Mobile 0424 450 396

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