Announcement

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

  • Problem with if command and if condition

    I was trying to use the -if- condition to specify my regressions, and at first I did reg y x if dummy==0 and reg y x if dummy==1. I wanted to shorten my code so I tried the -if- command: if dummy==0 {reg y x} and if dummy==1 {reg y x}. The first problem I ran into was that the second if command wouldn't run: Stata didn't return any errors but no results showed up. I then changed the second -if- to -else- but the same thing happened, no errors and no results. While trying to identify what exactly went wrong, I switched the order of my specifications & used a simple command like displaying summary statistics, but the problem is always this: the -if- command concerning the same variable run after the first one does not work.

    There is also one other question: I get different regression results when using the -if- condition and the -if- command. I saw a similar question with using -keep- and the -if- condition, but it didn't seem to have happened with anyone else. What could be the reason for this?
    Last edited by Callan Chen; 09 Mar 2025, 05:40.

  • #2
    The if command and the if qualifier are not different ways to do the same thing.

    The if command enforces one decision; the if qualifier on a command enforces a separate decision for each observation.

    In essence your if commands

    Code:
    if dummy == 0
    if dummy == 1
    are interpreted as

    Code:
    if dummy[1] == 0
    if dummy[1] == 1
    i.e. the check is for the first observation only.

    Now it's possible that dummy takes on values other than 1 and 0 (e.g. missing) but that possibility can be thought about as an extension of the following explanation

    Let's suppose that dummy[1] is really 0. Then the test dummy[1] == 0 yields true, and the following command will be executed for the entire set. Conversely the test dummy[1] == 1 yields false and nothing will happen.

    Naturally the principle is the same but the actions reversed for other values of the variable. One possibility is that the entire dataset is used and the other possibility is that nothing happens. That is not a bug: you got what Stata thought you were asking for.

    This behaviour has surprised many users, but it is documented and explained. The 2005 FAQ

    FAQ . . . . . . . . . . . . . . . . . . . . if command versus if qualifier
    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . J. Wernow
    4/05 I have an if or while command in my program that
    only seems to evaluate the first observation.
    What's going on?
    https://www.stata.com/support/faqs/p...-if-qualifier/


    to me has question and answer the wrong way round, as the question is usually that the if command gives me puzzling results, and the answer is that it only evaluates using the first observation. More recently Clyde Schechter and I wrote another explanation

    SJ-23-2 st0721 . When to use the if qualifier and when to use the if command
    . . . . . . . . . . . . . . . . . . . . N. J. Cox and C. B. Schechter
    Q2/23 SJ 23(2):589--594 (no commands)
    discusses generally when you should use either the if
    qualifier or an if command and specifically flags a
    common pitfall in using the if command

    https://journals.sagepub.com/doi/pdf...6867X231175349 works for me as a pdf.

    If you can't get access to that, the FAQ should be easily visible.

    Executive summary: The if command is most useful for scalar comparisons (e.g. of a number or string and a particular value).
    Last edited by Nick Cox; 09 Mar 2025, 06:11.

    Comment


    • #3
      Originally posted by Nick Cox View Post
      The if command and the if qualifier are not different ways to do the same thing.

      The if command enforces one decision; the if qualifier on a command enforces a separate decision for each observation.

      In essence your if commands

      Code:
      if dummy == 0
      if dummy == 1
      are interpreted as

      Code:
      if dummy[1] == 0
      if dummy[1] == 1
      i.e. the check is for the first observation only.

      Now it's possible that dummy takes on values other than 1 and 0 (e.g. missing) but that possibility can be thought about as an extension of the following explanation

      Let's suppose that dummy[1] is really 0. Then the test dummy[1] == 0 yields true, and the following command will be executed for the entire set. Conversely the test dummy[1] == 1 yields false and nothing will happen.

      Naturally the principle is the same but the actions reversed for other values of the variable. One possibility is that the entire dataset is used and the other possibility is that nothing happens. That is not a bug: you got what Stata thought you were asking for.

      This behaviour has surprised many users, but it is documented and explained. The 2005 FAQ

      FAQ . . . . . . . . . . . . . . . . . . . . if command versus if qualifier
      . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . J. Wernow
      4/05 I have an if or while command in my program that
      only seems to evaluate the first observation.
      What's going on?
      http://www.stata.com/support/faqs/programming/
      if-command-versus-if-qualifier/


      to me has question and answer the wrong way round, as the question is usually that the if command gives me puzzling results, and the answer is that it only evaluates using the first observation. More recently Clyde Schechter and I wrote another explanation

      SJ-23-2 st0721 . When to use the if qualifier and when to use the if command
      . . . . . . . . . . . . . . . . . . . . N. J. Cox and C. B. Schechter
      Q2/23 SJ 23(2):589--594 (no commands)
      discusses generally when you should use either the if
      qualifier or an if command and specifically flags a
      common pitfall in using the if command

      https://journals.sagepub.com/doi/pdf...6867X231175349 works for me as a pdf.

      If you can't get access to that, the FAQ should be easily visible.

      Executive summary: The if command is most useful for scalar comparisons (e.g. of a number or string and a particular value).
      I see what you mean. I will check out these explanations, thanks a lot!

      Comment

      Working...
      X