Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

From   Nick Cox <>
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


On Wed, Sep 7, 2011 at 8:45 AM, James Sams <> 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:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index