Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Deleting observations with missing values using macro/looping

    Hi all,

    I am very new to macros and looping (in Stata). I have certain variables for which it is necessary to have non-missing values to include them in regression. I used the following code to drop those observations with missing values of the assigned variables (under essentials), but it says it is an invalid syntax. Am I doing something wrong?

    Code:
    local essentials resp_edu_2014 resp_edu_2016 haschild_2014 haschild_2016 eqk2_2016 inc_asp_2014 inc_asp_2016 ast_asp_2014 ast_asp_2016 edu_asp_2014 edu_asp_2016 stat_asp_2014 stat_asp_2016 ln_oil_2014 ln_oil_2016 ln_sugar_2014 ln_sugar_2016 ln_salt_2014 ln_salt_2016
    foreach var in `essentials' {
        drop if missing(`var')
    }
    I also tried the following:

    Code:
    local essentials resp_edu_2014 resp_edu_2016 haschild_2014 haschild_2016 eqk2_2016 inc_asp_2014 inc_asp_2016 ast_asp_2014 ast_asp_2016 edu_asp_2014 edu_asp_2016 stat_asp_2014 stat_asp_2016 ln_oil_2014 ln_oil_2016 ln_sugar_2014 ln_sugar_2016 ln_salt_2014 ln_salt_2016
    foreach var in `essentials' {
        drop if `var'==.
    }
    Same goes when I do set the variables as global instead of local, and use $var instead of `var'.

    Thanks!
    Last edited by Samyam Shrestha; 14 Apr 2019, 10:57.

  • #2
    I do not see anything wrong with the syntax in the first code block, and I get no errors from it when I try to run it on a toy data set containing those variables. If somewhere in that list of essential you have mistyped the name of a variable or included some variable that does not exist, then you would get a ".... not found" error message. So perhaps you should recheck that list and scrutinize it for an error.

    The second one might fail if any of those variables is a string variable. Then you would get a "type mismatch" error.

    But I can't coax an "invalid syntax" error out of either one. Are you sure you are showing the exact code you ran?

    By the way, using $var instead of `var' would never work. The index of a -foreach- loop is always a local macro, even if the list over which it iterates is global.

    And you shouldn't use global macros for such lists anyway.

    Edit: Also, do you really need to do this in the first place? If these variables are part of the regression itself, Stata will automatically exclude any observations that contain missing values for any of the variables. This is only necessary if these are variables that are not, themselves, part of the regression.

    Comment


    • #3
      Hi Clyde, thanks for your reply.

      The codes work, until the very last line with "}", when it gives that invalid syntax response. Yes, I am showing the exact code I ran.

      I just tried to use the looping in this case, to practice it/learn in the process.

      Not only this, but I also used the following code for top-coding, using macro/looping, and it still gives the same invalid syntax output:

      Code:
      *Top-coding for current and aspired incomes at 99th percentile.
      
      local topcodevar1 inc_curr_2014 inc_curr_2016 inc_asp_2014 inc_asp_2016
      
      foreach var of topcodevar1 {
          egen temp=pctile(`var'), p(99)
          replace `var'=temp if `var'>temp
          drop temp
      }

      Comment


      • #4
        -foreach var of topcodevar1- is not legal syntax. You can do either of the following:

        Code:
        foreach var of local topcodevar1 {
        
        // OR
        
        foreach var in `topcodevar1' {
        I can think of a possible explanation for the problems you were experiencing with the apparently correct code in #1. Where did this code come from? If it was originally in a Word document, or, sometimes, even pasted from the Forum editor, the code may be contaminated with non-printing characters that the other document format uses. These will not be visible to your eye, but Stata sees them and they are not legal Stata syntax. Try deleting the commands from your do-file and retyping them manually and see if that fixes the problem. I've encountered this myself on occasion.

        Comment


        • #5
          That works, Clyde! And I manually typed the first code (missing value) again, and it works now. A crucial lesson learned! Thank you, Dr. Schechter!

          Comment

          Working...
          X