Announcement

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

  • If command being ignored depending on a dataset being in memory or not

    Hello!
    I was running a series of regressions, with some if and else statments, when I realized that Stata wasn´t following the if and else statements as I assumed it would. I started the debugging process by using some displays and making the code simpler. I even made generated a new dataset with code to post here, so that the problem could be fully replicated, only to realized that the code worked fine in that scenario. I then realized that having a dataset already loaded in the memory changed how Stata would run the code.

    The following two blocks of code are the same, with the exception that I first load a dataset into memory (also works with others, and different Stata versions, in different computers).

    Block 1 (does not run the code as it is suppose to):

    Code:
    clear all
    webuse auto, clear
    
    capture gen a1 = .
    capture gen a2 = .
    capture gen b1 = .
    capture gen b2 = .
    
    foreach qwe of varlist a1 {    
    foreach asd of varlist b1 b2  {
        di "`qwe'"
        di "`asd'"
            if `qwe' == a2 {    
                    di "Running 1"
            }
            
            else {
                    di "Running 3"            
            }
        }
    }
    Block 2 (Follows the correct if statement, but requires the memory to be clear).
    Code:
    clear all
    
    capture gen a1 = .
    capture gen a2 = .
    capture gen b1 = .
    capture gen b2 = .
    
    foreach qwe of varlist a1 {    
    foreach asd of varlist b1 b2  {
        di "`qwe'"
        di "`asd'"
            if `qwe' == a2 {    
                    di "Running 1"
            }
            
            else {
                    di "Running 3"            
            }
        }
    }
    The correct output would be "Running 3", as `qwe' is never == a2. Any suggestions on what could be causing this?

    Thanks a lot of your help!

    Best,
    Hélder

  • #2
    The if command given a variable name as you have done tries to compare its value with some other specified value. It then just looks at the first observation in the dataset. See https://journals.sagepub.com/doi/epu...6867X231175349 or (if access to that is difficult) https://www.stata.com/support/faqs/p...-if-qualifier/

    When you have data in memory, and set certain variables all to missing, then they are equal to each other -- generally, but also in the first observation, which is what counts.

    I can't easily work out, however, exactly what you want to do. Perhaps it is to test the variable name, not its value. If so, you should specify the variable name as a string in "". That is, you may need a test like

    Code:
    if "`qwe'" == "a2" 

    Comment


    • #3
      Thank you very much Nick, using the
      Code:
      if "`qwe'" == "a2" 
      solved the problem! Indeed I was trying to have the test the variable name.

      Best,
      Hélder

      Comment

      Working...
      X