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, statalist.org is already up and running.


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

Re: st: identifying variables


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: identifying variables
Date   Mon, 9 Apr 2012 10:33:12 +0100

Note also that a solution from first principles is also possible:

foreach v of var * {
        capture if strpos(`v'[1], "real") local vars `vars' `v'
}

di "`vars'"

You need the -capture- unless all the variables are string.

Nick

On Mon, Apr 9, 2012 at 8:57 AM, Nick Cox <njcoxstata@gmail.com> wrote:
> You have provided yet another example of a problem in which -strpos()-
> offers a simple alternative to -regexm()-.
>
> That aside, your syntax is looking for the string "real" in the
> _variable names_ themselves, for which
>
> . findname *real*
>
> would be a more obvious syntax, and indeed -describe *real*- would
> work fine. Stata has a similar distinction in all string and regular
> expression functions between looking in a string variable strvar and
> looking in a variable name "varname".
>
> . l in 1
>
>     +-------------------------------------------+
>     |     foo       bar     bazz    y         x |
>     |-------------------------------------------|
>  1. | reality   surreal   unreal   42   3.14159 |
>     +-------------------------------------------+
>
> . findname in 1, all(strpos(@, "real"))
> foo   bar   bazz
>
> . findname in 1, all(regexm(@, "[real]"))
> foo   bar   bazz
>
> . findname in 1, all(regexm(@, "real"))
> foo   bar   bazz
>
> @ is the default generic varname. -placeholder()- lets you specify an
> alternative. You only need to specify "@" for searching the variable
> names, but as above there are easier ways to do that.
>
> Nick
>
> On Mon, Apr 9, 2012 at 3:50 AM, Abhimanyu Arora
> <abhimanyu.arora1987@gmail.com> wrote:
>
>> I need to identify the variables for which the first observation
>> contains the string "real".
>> I used Nick Cox's -findname- from the SSC (updated 27 February 2012)
>> in the following command.
>>
>> findname var1-var56 in 1,all(regexm("@","real+")) loc (varnames)
>>
>> But since -return list- comes up empty, something is amiss, perhaps in
>> the use of the regular expression. May I also request some clarity in
>> understanding of the role played by a "placeholder" in -findname-? As
>> I see it, a generally character designated as a "placeholder" is
>> supposed to take on supplied values—string or numerals. Or in other
>> words, how would stata read the "@" in the above command?
>>

*
*   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/


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