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]

From |
Nick Cox <njcoxstata@gmail.com> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: st: Use extended functions outside of macro assignment? |

Date |
Wed, 7 Sep 2011 09:48:45 +0100 |

I don't think I am misunderstanding how Stata works here or even what you wrote, but you need to take my two emails together. Stata won't understand your syntax in the way you seem to want it to. -if- as a condition is evaluated as true or false for each observation. It thus selects observations and does not select variables. In particular, something that is true regardless of which observation is being considered selects all observations. A test on a value label is one such example. As I guess you know regexm("`: label (varname)'", "my_regex") isn't legal even schematically as there needs to be at least another argument regexm("`: label (varname) #'", "my_regex") You'd have to do that in some other way, say decode varname, gen(svarname) list if regexm(svarname, "my_regex") or by looping over the possible value labels. I don't know what is especially tedious about a two-step. The speed of -ds- for example depends on the number of variables, not the size of the dataset, and its memory imprint is not usually high. I would be happy to discuss how to tackle other examples or other solutions closer to what you really want when you can think of them. Meanwhile, check out SJ-4-4 dm0009 . . . . . . . Stata tip 14: Using value labels in expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . K. Higbee Q4/04 SJ 4(4):488--489 (no commands) tips for using value labels in expressions Nick On Wed, Sep 7, 2011 at 8:45 AM, James Sams <sams.james@gmail.com> wrote: > On Wednesday, September 07, 2011, you wrote: >> The logic of -list- with -if- is like any other command. Consider >> >> list if 2 == 2 >> >> 2 == 2 is (vacuously) true when considered for observation 1, for >> observation 2, and so on, so every variable and every observation will >> be -list-ed. The same will be true of your syntax. If your condition >> is true, i.e. there is a match, then everything will be listed. > > No, you are misunderstanding. Take the latter example, id is a variable, ids is > a local macro list. The conditional would be re-evaluated for each > observation's value of the 'id' variable. Same for the regexm example. > (repasted for reference). > >> > Something like this: >> > list if regexm("`: label (varname)'", "my_regex") > >> > list if `: list posof id in ids' > > Doing this in a two step process often gets tedious due to the size of the > data-set and memory limitations. Being able to operate on value labels directly > as though they were the value of the variable would be particularly helpful. > > Also, list was an example. I am rarely interested in actually listing data. > I've got a class of problems that I am trying to solve and being able to > dynamically invoke extended functions in the manner described would be helpful. > If it is not possible, so be it. But I wanted to make sure my question was > clear first. I thought I had seen someone doing something very close to this at > some point but cannot find it again. * * 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/

**References**:**st: Use extended functions outside of macro assignment?***From:*James Sams <sams.james@gmail.com>

**Re: st: Use extended functions outside of macro assignment?***From:*Nick Cox <njcoxstata@gmail.com>

**Re: st: Use extended functions outside of macro assignment?***From:*James Sams <sams.james@gmail.com>

- Prev by Date:
**st: Question about FE/RE and IV** - Next by Date:
**RE: Re: st: Bootstrapping & clustered standard errors (-xtreg-)** - Previous by thread:
**Re: st: Use extended functions outside of macro assignment?** - Next by thread:
**Re: st: Use extended functions outside of macro assignment?** - Index(es):