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   Eric Booth <[email protected]>
To   "<[email protected]>" <[email protected]>
Subject   Re: st: Confirm existence of a variable(s) with wildcards in the variable name
Date   Wed, 2 Feb 2011 03:05:08 +0000

<>

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


- Eric

__
Eric A. Booth
Public Policy Research Institute
Texas A&M University
[email protected]
Office: +979.845.6754


On Feb 1, 2011, at 8:12 PM, Will Probert wrote:

> Hi,
> 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'
> 
> Any help would be much appreciated.
> Regards,
> Will Probert
> 
> Research Assistant
> Institute for Social Science Research
> The University of Queensland
> 
> *
> *   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/


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