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   "Ada Ma" <[email protected]>
To   [email protected]
Subject   Re: st: How to consecutively add elements to a macro
Date   Thu, 3 Mar 2011 01:28:21 +0000

One solution is to use global instead and a global can refer to itself - e.g.

global readyvar birthday
global readyvar $readyvar gender
global readyvar $readyvar smoking

ds $readyvar


Sent using BlackBerry®

-----Original Message-----
From: Thomas Speidel <[email protected]>
Sender: [email protected]
Date: Wed, 02 Mar 2011 14:53:07 
To: <[email protected]>
Reply-To: [email protected]: st: How to consecutively add elements to a macro

 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/

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