Bookmark and Share

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: Confirm existence of a variable(s) with wildcards in the variable name


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: Confirm existence of a variable(s) with wildcards in the variable name
Date   Wed, 2 Feb 2011 10:04:41 +0000

Perhaps I got this the wrong way round, but the spirit applies to
-keep- as well as -drop-.

On Wed, Feb 2, 2011 at 9:13 AM, Nick Cox <[email protected]> wrote:
> I don't understand the need for circumspection here. If you try the
> direct approach
>
> . drop vari*
>
> the -drop- won't work if there are no matching variable names. If you
> are worried about this stopping a do-file or program that you want to
> continue regardless, one of
>
> . cap noi vari*
>
> . noi cap vari*
>
> should meet the purpose.
>
> Mind you,
>
> 1. It would be very poor style in a public program (not that you're
> proposing that) and dubious style in a private program to do this.
>
> 2. I'd be cautious about doing this even in a .do file. Depends what
> you want to do, but there may be better ways to do it. Putting it in a
> -drop- because it might be needed sounds as if you could bite you.
> That said, the context might well make clear why this is perfectly
> reasonable.
>
> Nick
>
> On Wed, Feb 2, 2011 at 3:05 AM, Eric Booth <[email protected]> wrote:
>>
>> Following along with the -confirm- and -unab- help files, I first wrote this code:
>>
>> *****************!
>> sysuse auto, clear
>>
>> local myvarlist   z* m* r* price for foo bar
>> *note:    z*, foo, and bar are fake variables*
>>
>>
>> foreach v in `myvarlist' {
>>        loc j     // this resets j where the variable does not exist
>>                    // otherwise you get duplicates in your varlist
>>        cap unab j: `v'
>>        foreach x in `j' {
>>        cap confirm variable `x', exact
>>        if !_rc {
>>                loc newvarlist  `newvarlist' `x'
>>                }
>>          }
>>        }
>> di in y "`newvarlist'"
>> *****************!
>>
>> but then I realized that the "cap unab j: `v'" was doing the work of filtering out fake variables from getting into the 'newvarlist' local before the "capture confirm" loop that I usually use for building these types of varlists had a chance to do its work.
>> So,  I reduced it to the following snippet.  However, relying on -capture- alone (& leaving out the -confirm- loop) feels clumsy to me -- but I can't think of a condition that would make it fail.  Others will probably have more straightforward solutions.
>>
>> ****snippet****
>> foreach v in `myvarlist' {
>>        loc j
>>        cap unab j: `v'
>>                loc newvarlist  `newvarlist' `j'
>>        }
>> ****snippet****
>>
>
>> On Feb 1, 2011, at 8:12 PM, Will Probert wrote:
>
>>> I'm trying to unabbreviate a variable list that contains a wildcard
>>> and then keep those variables, if the variable(s) exist.  From my
>>> understanding, the 'if' option in STATA's 'keep' and 'drop' commands
>>> only act on observations, not on variables.
>>>
>>> I'm trying code, unsuccessfully, along the lines of...
>>>
>>> capture confirm unab new: varia*
>>> if(_rc==0){
>>>      unab new: varia*
>>>      local newlist `new'
>>> }
>>> keep `newlist'
>

*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index