Announcement

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

  • If-Else Issue

    Hello All,

    I have a rather interesting issue. I am attempting to use some if-else statements for some conditions within a foreach loop. Here is the issue. The syntax that I am posting works beautifully. When I take it to my data, STATA will only read and perform the condition in the first "if" statement. To be clear, the syntax #1 works perfectly, but when I try this with my data, using syntax #2, STATA will only read the first "if" statement and not the second one. I first thought it was a data formatting problem, but the auto.dta variables are: int and byte formats. My data are both byte format. Any ideas how to get this to consistently work across all data?

    Syntax #1
    sysuse auto.dta, clear
    local dv foreign

    foreach var of varlist `dv' {
    if `var' == mpg {
    di 2+2+4/2
    }
    else if `var' == foreign {
    di 10/5
    }
    }

    Syntax #2.
    local dv victim

    foreach var of varlist `dv' {
    if `var' == violence {
    di "violence"
    }
    else if `var' == victim {
    di "victim"
    }
    }

    Any help is welcomed. Thank you.

  • #2



    In principle we have to guess what is happening in #2 as we have no idea what dataset is implied. But it can be done.

    That one syntax works as you expect and the other doesn't is an accident. They are equally misconceived from Stata's point of view.

    First, an if command such as

    Code:
    if foreign == mpg
    will be interpreted as a test of whether the values of those variables are equal and -- this is odder until you know the rule -- concretely as a test of whether foreign[1] == mpg[1]. which isn't true in the auto dataset.

    Naturally it is true tautologically that


    Code:
    if foreign == foreign
    is true and the fact that that means specifically


    Code:
    if foreign[1] == foreign[1]
    does not affect the case.

    This should not surprise as I imagine that you are utterly familiar with the fact that a qualifier like

    Code:
    regress mpg weight if foreign == 1
    includes a test of the values of foreign, and although the if qualifier and the if command are different, they are similar in this respect.


    The problem is that you want to test whether the names supplied are equal, which is an utterly different problem, requiring a comparison of whether two strings are identical.


    Code:
    sysuse auto.dta, clear
    local dv foreign
    
    foreach var of varlist `dv' {
    if "`var'" == "mpg" {
    di 2+2+4/2
    }
    else if "`var'" == "foreign" {
    di 10/5
    }
    }
    is, I guess, closer to your intent.

    Use of the first observation in interpreting an if command is documented at https://www.stata.com/support/faqs/p...-if-qualifier/ To my mind the FAQ has it backwards, as the usual question is, as here, why is the if command not working as I expect, to which one answer is because it is looking at the values of the first observation only, which is not what you want.

    P.S. https://www.statalist.org/forums/help#spelling

    Comment


    • #3
      I had just constructed an essentially similar post to Nick's and then discovered we were about to cross in the ether but I'd add one thing to his explanation:

      I'd also suspect here that there might be a better (more "Stata-ish") way to do whatever you wanted to do in the original context in which this problem arose, so you might want to post back with a bit of information about that.

      Comment


      • #4
        Yes, this is closer to my intent. I tried this and it moves to the second if command now, but it is still trying to run the first if command, and I have it exactly like your syntax. I will have to work on this some more and think this through. Thank you for your help and my sincerest apologies for my poor spelling of Stata.

        Comment


        • #5
          Understood. As Mike Lacy flagged, it would help to know quite what you are trying to do here, as wiring in specific variable names to code is not obviously a good programming tactic. Certainly if you can't get your code to work we need to see again. what you've written.

          Comment

          Working...
          X