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   Maarten Buis <[email protected]>
To   [email protected]
Subject   Re: st: catching variable mismatch in the data
Date   Fri, 13 Sep 2013 13:10:34 +0200

*------------------ begin example ------------------
// create some example data
clear
set obs 10
gen var1    = runiform()
gen var4001 = runiform()
gen var2    = runiform()
gen var2a   = runiform()
gen id = _n

// get a list of variables starting with "var"
capture unab vars : var*
if _rc {
    di as txt "no variables starting with var"
}
else {
    // remove the string "var" from each varname
    local vars : subinstr local vars "var" "", all

    foreach var of local vars {
        // what is left of the varname should be a number
        capture confirm number `var'
        if _rc {
            local ambiguous "`ambiguous' var`var'"
        }
        else {
            // larger than 4000 = foreign
            if `var' > 4000 {
                local foreign "`foreign' var`var'"
            }
        }
    }
}

if "`ambiguous'" != "" {
    di as txt "ambiguous variables: " ///
       as result "`ambiguous'"
}
if "`foreign'" != "" {
    di as txt "foreign variables: " ///
       as result "`foreign'"
}
*------------------- end example -------------------
* (For more on examples I sent to the Statalist see:
* http://www.maartenbuis.nl/example_faq )

Hope this helps,
Maarten


On Fri, Sep 13, 2013 at 12:35 PM, 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/



-- 
---------------------------------
Maarten L. Buis
WZB
Reichpietschufer 50
10785 Berlin
Germany

http://www.maartenbuis.nl
---------------------------------

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