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

From |
Ulrich Kohler <kohler@wz-berlin.de> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: st: RE: Macro questions. (Again). |

Date |
Wed, 11 Aug 2004 16:02:23 +0200 |

Use the extended macro function -subinstr-. For example, if you define the local macro "macro" as follows: . local macro "var1 var2" You can replace "var1" in this macro with an empty string like follows: . local macro: subinstr local macro "var1" "" hope this helps uli adiallo5@worldbank.org wrote: > Nick, > When I talk about working inside macros, I am thinking of something > like this: > I know that one can increment a macro by doing: local myvars "`myvars' > var1" But why can't we REMOVE var1 from the macro myvars? > Something like (it's just an example, but maybe you have another syntax): > > local myvars "`myvars' , -var1" > > (where -var1 is intented to tell Stata to remove var1 from myvars). > > This could sound odd but sometimes, macros contain a lot of variables and > it is painfull to create other macros that are just part of them (what I > call "submacors"). > > Best regards. > Amadou. > > > > > > adiallo5@worldbank.org > Sent by: To: > statalist@hsphsun2.harvard.edu owner-statalist@hsphsun2. cc: > owner-statalist@hsphsun2.harvard.edu, statalist@hsphsun2.harvard.edu > harvard.edu Subject: Re: st: RE: Macro questions. > > > 08/10/2004 06:59 PM > Please respond to > statalist > > > > > > > > > Thanks Nick. > This will spare me a lot of effort and time. > And...it is true I love Stata macros. > It makes life so easier... when one > get used to them. > Have a good night by the way. > Amadou. > > > > > "Nick Cox" > > <n.j.cox@durham.ac.uk> To: > <statalist@hsphsun2.harvard.edu> > Sent by: cc: > > owner-statalist@hsphsun2. Subject: st: RE: > Macro questions. > harvard.edu > > > > 08/10/2004 06:51 PM > > Please respond to > > statalist > > > > > > > > > > adiallo5@worldbank.org > > > I have some questions about macros. > > > > 1/ I wonder how to count the number of variables in a macro. > > I try the macro extended function (word count) but it doesn't work. > > For example, if I have: > > local myvars "var1 var2 var3 var4" > > then `myvars' contains 4 variables. > > But I do not know how to create a macro that contains the value 4. > > I go > > . local myvars "var1 var2 var3 var4" > > . local nv : word count `myvars' > > . di `nv' > 4 > > and Stata tells me 4. (The macro contains not four > variables, but the names of four variables.) > What are you doing wrong? I have to guess. > Perhaps you are doing something like > > . local nv : word count "`myvars'" > > . di `nv' > 1 > > Here you must remember Stata's definition of > a word. Stata by default parses on white space meaning > that > > Amadou loves Stata macros > > would be considered by Stata to be 4 words. So far, > so good. But Stata has another rule: double quotes > bind tighter than white space separates, so > > "Amadou loves Stata macros" > > is just _one_ word. This is just Stata's sense of > a word; forget here what you know about the grammar of > any ordinary language. Now, when you defined > > . local myvars "var1 var2 var3 var4" > > the " " were just ornamental; Stata took > them as delimiters, not as part of your > definition of the local myvars (and you > could have omitted them). But if you go > > . local nv : word count "`myvars'" > > the " " are not ornamental at all; they > are working hard to bind what follows > -: word count- into one word. > > > 2/ Suppose several datasets containing the same variable floor indexed > > 1, 2, 3, etc.... Floor* number varies with datasets. > > I want to create the macro myfloor > > local myfloor "floor*" > > but then, I do not know how to create the macro containing > > the number of floor > > as in question 1. > > This question was asked earlier today. As said then, > the number of variables is not often an issue. Nevertheless, > > unab floor : floor* > local nfloor : word count `floor' > > > 3/ How to work "inside" macros? > > suppose you have the following: local "var1 var2 var3 .....varN" > > suppose later in your dofile you want to do some tables on > > only - say 20- > > variables of the macro. > > Is it possible to just choose them (perhaps by specifying > > their position IN > > the macro) > > without creating other (sub)macros? > > The term submacro is yours; I don't know what you mean by it. > There are several ways of doing this, but they usually > involve creating other macros. > > > 4/ Finally my last question is how to merge/append several datasets? > > Suppose you have : > > local mydata "data1 data2 data3 data4 data5" > > foreach if i of local mydata { > > foreach j of local mydata { > > append `i' using `j' > > } > > } > > > > How to avoid data1 being appended twice? > > Various ways. Here's one. > > Start with the second word in the macro. > > local mydata "data1 data2 data3 data4 data5" > local nsets : word count `mydata' > tokenize `mydata' > > use `1' > forval i = 2/`nsets' { > append using ``i'' > } > > * > * 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/ -- kohler@wz-berlin.de +49 (030) 25491-361 * * 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/

**References**:**Re: st: RE: Macro questions. (Again).***From:*adiallo5@worldbank.org

- Prev by Date:
**Re: st: RE: Macro questions. (Again).** - Next by Date:
**Re: st: RE: Macro questions. (Again).** - Previous by thread:
**Re: st: RE: Macro questions. (Again).** - Next by thread:
**Re: st: RE: Macro questions. (Again).** - Index(es):

© Copyright 1996–2015 StataCorp LP | Terms of use | Privacy | Contact us | What's new | Site index |