Announcement

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

  • What is the function of using parenthesis () with gen?

    Hi all,

    I'm wiriting some stata code and in one feedback from my prof, he add this ( ) parenthesis after gen,ì. I know if I want to gen dummy variable with some condition, say 1 if a==b, I can use
    Code:
     gen x = (a==b)
    , but in my code, I'm just doing subtraction. Can anyone tell me the difference(with/without())?

    My code is as follow:
    Code:
    foreach var of varlist share_* {
            bysort sector_id(year): gen c`var' = (`var'[_N] - `var'[1])
        }
    I'm refering to the second pair of parenthesis after gen

    Thanks!!

  • #2
    No difference in this case.
    However, from programming syntax and etiquette, I believe it's better to include the parenthesis to avoid errors in more complex operations, making your code more readable.
    And from experience, is better to start with good programing habits early on

    Comment


    • #3
      In this case, omitting the parentheses will give the same result as including them, but they may be necessary in other instances. In your first math classes, you were taught that the order of operations matter. Depending on where you grew up, you may be familiar with the acronyms BODMAS, BIDMAS or PEMDAS. Therefore, you have the expression:

      Code:
      di 2+7*5/3-16
      and your goal is first to add 2 to 7 and multiply by 5 before dividing by the result of 3 minus 16. Parentheses will help you to achieve this without having to know the order of operations:

      Code:
      di 2+7*5/3-16
      di ((2+7)*5)/(3-16)
      Res.:

      Code:
      . di 2+7*5/3-16
      -2.3333333
      
      . di ((2+7)*5)/(3-16)
      -3.4615385
      Notice that the results differ. The same logic extends to Stata's logical operators, e.g., -and- ("&"), -or- ("|"), -not- ("!") and so on.

      Code:
      help operators
      With multiple logical operators, order does also matter.
      Last edited by Andrew Musau; 27 Jul 2022, 12:04.

      Comment


      • #4
        The parentheses to the right of the equals sign make no difference to Stata in either case.

        Your Professor didn't need to add parentheses as far as Stata is concerned, so you need to ask him why he prefers to do that. It might be a habit carried over from other software.

        You may need parentheses to subvert Stata's precedence rules

        Code:
        . display 1 + 2 * 3 + 4
        11
        
        . display 1 + 2 * (3 + 4)
        15
        and you may prefer to use parentheses whenever you think that they improve clarity. If anyone prefers to think that

        Code:
        (a == b)
        as an entire expression looks better that way Stata maintains diplomatic silence. On the other hand,

        Code:
        a == b & c == d
        is the kind of expression I would always parenthesise for clarity

        Code:
        (a == b) & (c == d)
        as I know what I want and I don't want to check Stata's rules to seek if Stata thinks of an expression as I do.

        I couldn't pass a quiz on Stata's precedence rules, but crudely speaking whatever you're used to in mathematics is fine in Stata, unless someone can think of a horrible example.

        EDIT Seems consistent with #2 and #3. What we tell you three times is true. https://en.wikiquote.org/wiki/The_Hunting_of_the_Snark
        Last edited by Nick Cox; 27 Jul 2022, 12:05.

        Comment


        • #5
          Hi thank you all for the answering! I was just worried if I miss something and not sure if I should bother the prof for this.

          Comment


          • #6
            So you bothered Statalist instead!

            Comment


            • #7
              Sorry I don't mean this

              Comment


              • #8
                Sure. Just joking!

                Comment


                • #9
                  Really grateful for all the help Statalist offers!
                  BTW: I'm trying to correct some bad coding habit

                  Comment

                  Working...
                  X