Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Nick Cox <n.j.cox@durham.ac.uk> |
To | "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu> |
Subject | RE: st: RE: Re: How to perform calculations on a set of tempvars |
Date | Sun, 26 Sep 2010 16:26:52 +0100 |
The article referred to by Martin gives a fairly detailed survey of working row-wise. Francisco: You do not substantiate your claim of not working properly. I see no reason why any row-wise function should not work properly with varlists given by macros. You just need to respect the syntax of each function. In particular, -min()- expects a set of variable names separated by commas, whereas the -egen- row functions expect varlists as strictly so defined in Stata, space-separated or wildcarded. Now wildcards are not completely out of the question in Stata for temporary variables, but it is best to think otherwise. (Your earlier example inserted a wildcard symbol _inside_ a macro name, which as you saw is illegal. Putting wildcard symbols _outside_ macro names won't usually work either.) In either case (space or comma), it is often a good idea to accumulate variable names as you loop over some set of columns. For example, local J = <number of columns> forval j = 1/`J' { tempvar foo egen `foo' = <function of some variable to do with column `j'> local foolist `foolist' `foo' } As each new -foo- is generated, its name is added to the list. As is explicit above, `foolist' is a space-separated list. To insert commas, local cfoolist : subinstr local foolist " " ",", all Then feed `foolist' or `cfoolist' as desired to the function or command in question. If you feel more comfortable with the more complicated version local J = <number of columns> forval j = 1/`J' { tempvar foo`j' egen `foo`j'' = <function of some variable to do with column `j'> local foolist `foolist' `foo`j'' } that's fine. The key point either way is that each -tempvar- call produces a new, distinct variable name. So long as you keep track of the previously produced names you can remain in business. Nick n.j.cox@durham.ac.uk francesco manaresi Thank you, your answer (and the reference) was pretty useful. I ended up creating variables, however, since most of the "rowwise" functions do not seem to work properly with macros. On Sat, Sep 25, 2010 at 11:00 AM, Martin Weiss <martin.weiss1@gmx.de> wrote: > The -min()- function does not take a -varlist-, but a comma-separated list > of arguments, hence the error. Try -egen, rowmin()- instead: > http://www.stata-journal.com/article.html?article=pr0046 Francesco manaresi > I created sets of tempvars of the type > var_`i'_`x' > containing the result of a statistic (say, a p-value) calculated for > variable `x' in State `i' > > I would like to perform calculations and have the result in another > macro. For instance, I want the minimum of all the p-values by State. > However, Stata does not accept the following syntax: > > tempvar minimum > g `minimum' = min(`var_`i'_*') > > invalid syntax > r(198); > > I believe this is because Stata doesn't accept asterisks (jolly > character) within macronames. > Am I right? What can I do alternatively, apart from creating hundreds > of real variables? * * 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/