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

From |
adiallo5@worldbank.org |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: st: RE: Macro questions. |

Date |
Tue, 10 Aug 2004 18:59:35 -0400 |

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/

- Prev by Date:
**st: How to rename variables by adding a suffix** - Next by Date:
**st: Transforming numeric to categorical (not to string!) variables** - Previous by thread:
**st: RE: Macro questions.** - Next by thread:
**st: Transforming numeric to categorical (not to string!) variables** - Index(es):

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