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: catching variable mismatch in the data


From   Eric Booth <[email protected]>
To   [email protected]
Subject   Re: st: catching variable mismatch in the data
Date   Fri, 13 Sep 2013 07:38:03 -0500

<>

I had a typo in my last example (missing the bracket in the -if- loop), here's an updated example with the fix:


**************************!
sysuse auto, clear

**check it:
    cap noi ds var4??? 
     if `"`r(varlist)'"' != "" {
          display as err "There is a mismatch between variable names in the data"
          exit
            }		
			
			
			
rename mpg var4999
rename rep78 var41


      cap noi ds var4??? 
           if `"`r(varlist)'"' != "" {
           display as err "There is a mismatch between variable names in the data"
           exit
            }
			 
*more stuff that wont run now*
desc, sh
regress var41 t*
 *! this worked
**************************!

-- Eric


On Sep 13, 2013, at 7:28 AM, Eric Booth <[email protected]> wrote:

> <>
> 
> As in:
> 
> sysuse auto, clear
> rename mpg var4999
> rename rep78 var41
> 
>       cap noi ds var4??? //fixed
>            if `"`r(varlist)'"' != "" exit
>            display as err "There is a mismatch between variable names in the data"
>            exit
>             }
> 			 
> *more stuff that wont run now*
> desc, sh
> regress var41 t*
>  *! this worked
> 
> 
> - Eric
> 
> 
> 
> 
> On Sep 13, 2013, at 7:26 AM, Eric Booth <[email protected]> wrote:
> 
>> <>
>> 
>> It should read:
>> 
>>>          cap noi ds var4???
>> 
>> not 
>> 
>>>          cap noi ds v4???
>> 
>> 
>> 
>> HTH, 
>> Eric
>> 
>> 
>> 
>> 
>> On Sep 13, 2013, at 7:21 AM, Paul Byatta <[email protected]> wrote:
>> 
>>> Hi Eric, 
>>> 
>>> 
>>> Thanks for the help. 
>>> 
>>> 
>>> The correction you recommend seems to be working well, ie it is catching the presence of the variable/s as I want.
>>> 
>>> 
>>> The only hitch is that the dofile doesnt exit when the variables v4??? are present, despite there being an -exit- command. It displays the error message but continues with the rest of the commands in the dofile. What could I be doing wrong? Below is how I have incorporated the command lines.
>>> 
>>> 
>>>          cap noi ds v4???
>>>           if `"`r(varlist)'"' != "" {
>>>           display as err "There is a mismatch between variable names in the data"
>>>           exit
>>>            }
>>> 
>>> 
>>> Thanks
>>> Paul
>>> 
>>> 
>>> 
>>> ----- Original Message -----
>>> From: Eric A. Booth <[email protected]>
>>> To: [email protected]
>>> Cc: 
>>> Sent: Friday, September 13, 2013 2:07 PM
>>> Subject: Re: st: catching variable mismatch in the data
>>> 
>>> <>
>>> Hi Paul:
>>> It appears you want to stop your do file if there is any variable
>>> present that has a variable name of the pattern var4???.
>>> If you find them, then you want to stop your do-file (based on the use
>>> of the error message and -exit- in your loop).
>>> 
>>> Your loop is failing for a few reasons.  First , var4### is illegal;
>>> instead, you want var4???.  Also, the second line with 'capture
>>> confirm...' is irrelevant when  you start the loop with 'foreach x1 of
>>> varlist var4??? { '  because if there were no var4??? in your dataset
>>> the loop would stop there and say 'variable 4??? not found', having
>>> never reached the 'capture confirm...' lines nested below.
>>> 
>>> Instead, I think you can put these 2 lines in your do-file to get the
>>> functionality you seek:
>>> 
>>> **
>>> cap noi ds var4???
>>> if `"`r(varlist)'"' != "" exit
>>> **
>>> 
>>> - Eric
>>> 
>>> On Fri, Sep 13, 2013 at 5:35 AM, Paul Byatta <[email protected]> wrote:
>>>> Hi Statalist
>>>> 
>>>> I writing lines of commands that will alert me whenever there is a mismatch between variable names. I know there is a mismatch when the dataset contains a foreign variable name. I also know the foreign variable name/s will take the prefix of "var" and end with with a number greater than 4000 (because the total non-foreign variables are 4000).
>>>> 
>>>> The commands I have so far
>>>> 
>>>>            foreach x1 of varlist var4###{
>>>>            capture confirm variable `x1'
>>>>              if _rc != 0 {
>>>>                display as err "There is a mismatch between variable names in the data"
>>>>                exit _rc
>>>>              }
>>>> 
>>>> 
>>>> I know based on an earlier discussion on this list (Re: st: -confirm variable- does not accept varlist (wildcards)) that "capture confirm" command doesnt accept varlist, but does somebody have any alternative suggestions on how to programmatically achieve my goal above.
>>>> 
>>>> 
>>>> Thanks
>>>> Paul
>>>> 
>>>> 
>>>> *
>>>> *   For searches and help try:
>>>> *  http://www.stata.com/help.cgi?search
>>>> *  http://www.stata.com/support/faqs/resources/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/faqs/resources/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/faqs/resources/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/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


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