Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, is already up and running.

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

Re: st: Problem with wildcards

From   Nick Cox <>
Subject   Re: st: Problem with wildcards
Date   Tue, 5 Mar 2013 10:22:33 +0000

I don't think that Dimitriy's explanation is quite correct. It's easy
to agree that wildcard syntax doesn't work here, but there is a small
issue of precisely why not.

My way of thinking about it is that Stata looks at what you type from
left to right so that once Stata sees


it expects an expression to follow. An expression can't contain a
wildcard as far as Stata is concerned, so you are thrown out. An
expression -- in this context -- has to boil down to something that
evaluates to true or false (more generally nonzero or zero) for each
observation and that is not the case here. It's not that wildcards are
expanded to something that's wrong; I don't think they are expanded at
all. .

Another way of thinking about it is that wildcards are allowed where
general varlists are allowed. In essence if varlists are allowed
anywhere, that is explicit; if it is not explicit it should be assumed
to be forbidden. That's not deliberately gnomic or cryptic; it is my
best attempt to explain the syntax as I understand it.

Hence this is, as always, open to correction by anyone who's seen
inside the proprietary code.


On Mon, Mar 4, 2013 at 9:48 PM, Dimitriy V. Masterov <> wrote:
> Lisa,
> The reason that does not work is that it evaluates to
> replace anypsych = 1 if psych1 psych2 ... psych42 == 1
> That is is not valid syntax.
> You could use egen (extended generate) to do this:
> egen anypsych = rowmax(psych1 - psych42)
> On Mon, Mar 4, 2013 at 1:36 PM, Lisa Cook <> wrote:
>> Hi,
>> I'm having a problem using wildcards. I'm using Stata 11.2 for Windows.
>> I have a list of variables: psych1 - psych42. I would like to create a
>> new variable called "anypsych" that takes the value of 0 if all the
>> variables psych1-psych42 are 0 or missing, and takes the value 1 if
>> any of psych1-psych42 are 1.
>> My code:
>> gen anypsych = 0
>> replace anypsych = 1 if psych* == 1
>> I get the error "psych* invalid name".
>> Can anyone tell me why it's an invalid name and how I can achieve this
>> objective? I also have a bunch of other variables that I need to do
>> the same thing for, so advice on how to approach this would be a huge
>> help.
*   For searches and help try:

© Copyright 1996–2016 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index