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]
Re: st: identifying variables
From
Nick Cox <[email protected]>
To
[email protected]
Subject
Re: st: identifying variables
Date
Mon, 9 Apr 2012 11:07:54 +0100
You can do it this way
foreach v of var * {
capture if strpos(`v'[1], "real") {
local vars `vars' `v'
}
}
The { } are best called braces or curly brackets. Parentheses are
strictly (). See e.g.
http://www.catb.org/jargon/html/A/ASCII.html
for a useful glossary. (The entire site is superb.)
Nick
On Mon, Apr 9, 2012 at 10:56 AM, Abhimanyu Arora
<[email protected]> wrote:
> Very useful to know, Nick. Just to confirm, in general stata goes
> further into the command only if the _if_ condition returns 1. There
> is no need for parenthesis or a separate line for -local-. Thanks for
> this detail.
> Cheers
> Abhimanyu
>
>
> On Mon, Apr 9, 2012 at 11:33 AM, Nick Cox <[email protected]> wrote:
>> 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 <[email protected]> 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
>>> <[email protected]> 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/
>
> *
> * 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/
*
* 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/