Announcement

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

  • If Statement

    Hi,

    I am trying to use loop and "if statement" together instead of "if qualifier". You can see my 2 different codes below. I used if statement in the first one, and if qualifier in the second one. I am trying to make y and order equal when mod(order)=1. (Order is from 1 to 248) I can use mod command but need to use this code (with if statement) for some other purposes.

    First Code:

    forvalues i=1(5)248{
    if order==`i'{
    replace y= `i'
    }
    }

    Second Code:

    forvalues i=1(5)248{
    replace y= `i' if order==`i'
    }

    When I write it with if qualifier, it works. However, I did not understand why my first code did not work. (y's are all equal to 1)

    Can someone help me to shed light on this issue.

    Thanks in advance.

    Best,
    Ulas

  • #2
    The if command is only evaluated one time, not once for each observation of the dataset. And if any variables are used in the expression, the value for the first observation is used to evaluate the expression.

    The if command most definitely is not interchangeable with the if clause.

    Code:
    . sysuse auto, clear
    (1978 Automobile Data)
    
    . if !missing(make) {
    . display make
    AMC Concord
    . }
    
    . list make in 1
    
         +-------------+
         | make        |
         |-------------|
      1. | AMC Concord |
         +-------------+
    Last edited by William Lisowski; 22 Sep 2017, 18:51.

    Comment


    • #3
      Note that this is an FAQ.https://www.stata.com/support/faqs/programming/if-command-versus-if-qualifier/ To my mind that FAQ is mistitled: its title is the answer rather than the question most people have, but there you go.

      P.S. mod() is a function, not a command.

      In Stata commands and functions are quite distinct. There is no sense in which either word is a synonym for the other.

      This may seem a small point of wording, but it is important for communication that people use the right terminology. Writers should want people to think they are correct and readers should want to read posts that are correct.

      Comment


      • #4
        ulas alk

        On reflection, I think there's another comment worth making.

        When I replied in post #2, I failed to look beyond the direct question and example you gave in post #1. You did tell us that you intend to use the if statement for other purposes; the example was a simple one to convey the question. In my haste to answer, I overlooked that hint and answered your direct question, but did not address the real problem you need to solve.

        I now recognize what may be an attempt to write Stata code similar to code in Some Alternative Statistics package that I had a 35-year history with, although it's been a few years now since I last used it. Perhaps you are new to Stata and trying to leverage your background in that package, as I once did.

        There's a fundamental difference between that package and Stata. In Stata, data manipulation is not confined to a "step" that applies a succession of commands to each observation, one observation at a time. Instead, each Stata data manipulation command is applied to the entire dataset before the following command is applied to the entire dataset, and so forth. The sort of if block familiar from the other package is not used in that way in Stata.

        This might sound like a real problem, trying to replicate familiar processes from the other package. But Stata has features the other package does not, and it's often possible to "look at the problem from a different angle" to see efficient ways of writing your code. It took me a while to get there, but it's now second nature to me.

        If that's the case fo you, consider posting a more complete example of what you are actually trying to accomplish using the if command structure you hoped would work. It's likely someone will be able to show you the angle to look at the problem from that makes it more straightforward in Stata.

        To make your Statalist posts as clear as possible, please review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post, looking especially at sections 9-12 on how to best pose your question. In particular, please read FAQ #12 and use CODE delimiters when posting sample code or results to Statalist.

        Comment

        Working...
        X