Announcement

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

  • Using or and and in single if qualifier

    I would like to write something like:

    Code:
    replace rate=. if generator=="solar" & (rate<0 | rate>200)
    I know I could do this in two separate lines, but that seems inefficient, especially because I have many lines of similar code. I tried using if commands, but I would prefer something like the above qualifier if possible because I am more comfortable with it (and it takes less room).

    Thanks,
    Krista
    Last edited by Krista Lane; 11 Nov 2016, 14:34.

  • #2
    Krista, you just did and it is valid syntax so what is the problem?

    Code:
    . list
    
         +--------------+
         | rate     gen |
         |--------------|
      1. |   -1   solar |
      2. |  100   solar |
      3. |  300   solar |
         +--------------+
    
    . replace rate=. if gen=="solar" & (rate<0 | rate>200)
    (2 real changes made, 2 to missing)
    
    . list
    
         +--------------+
         | rate     gen |
         |--------------|
      1. |    .   solar |
      2. |  100   solar |
      3. |    .   solar |
         +--------------+

    Comment


    • #3
      I tried doing this in an if command and also had little success (it looks like Stata wasn't entering the if command loop). For example, I wrote:

      Code:
      gen test=0
      foreach yr of num 2015 2016 {
          if generator=="solar" {
              replace test=1
              replace rate`yr'=. if rate`yr'<0 | rate`yr'>200
          }
      }
      tab test revealed that test was never replaced with 1 even though the following command resulted in many replacements:

      Code:
      replace test=1 if generator=="solar"

      Again, I'd prefer to use the if qualifier if possible, but the if someone knows what mistake I'm making with the if command, that would also be tremendously helpful.

      Comment


      • #4
        Oh! I think I had a syntax error in my loop that was giving me an error. I thought it was because of the if qualifier but I guess it wasn't. Thanks for your help and sorry for the unnecessary post!

        Comment


        • #5
          Hello Krista,

          Did your code in #3 work as you wished? I'm asking because
          Code:
          if generator==""solar" {
              ...
          }
          will not automatically loop through your observations. That if statement will only check if your first observation is "solar." Therefore, if you have "wind" later down in your data, the if statement won't return false for those observations.

          Comment


          • #6
            Roger, no I switched back to using an if qualifier. That's a very helpful explanation. Would there be a way to do this with an if command?

            Comment


            • #7
              There are ways to make Stata loop through the if command (you would have to create a loop that iterated through all of the observations), but at least in this case, I'm not sure that would make any sense. The if qualifier should do everything that you want it to do. Is there a particular reason that you're trying to use a standalone if statement, rather than just tacking it onto the end of a command?

              Comment


              • #8
                Roger is giving very good advice.

                Note that Stata has no awareness of what is done in any other language. The difference between the if qualifier and the if command is documented. See e.g. http://www.stata.com/support/faqs/pr...-if-qualifier/

                Further, there is no tacit loop over observations associated with an if command.

                Comment


                • #9
                  Thank you both. No, it's clear that there is no reason that I should be using the if command. I just recently learned about the if command in Stata and thought it was more interchangeable with the if qualifier than it actually is.

                  Comment


                  • #10
                    My personal view is that the difference should be made more obvious in the help and manual entries.

                    For good reasons, StataCorp seems to dislike negative manual entries (i.e. X is Y. Note that X is not Z!) but many people have been bitten here -- especially if they are familiar with equivalents in other software.

                    Comment


                    • #11
                      Thanks!
                      Last edited by Krista Lane; 21 Nov 2016, 18:15. Reason: (I had an additional question that I resolved)

                      Comment

                      Working...
                      X