Search code examples
statagenerate

Parentheses when assigning variables in Stata


Does it make a difference if I have parentheses or not? Is this generating dummies or just assigning a value: generate byte h_et = ( hvet ) and generate byte h_et = hvet in Stata?


Solution

  • As in any language (I know about) that allows parentheses, the vacuous answer is that parentheses make a difference if and only if they yield a result differing from what precedence of operators would yield.

    Although your question is in terms of generate, examples using display (di is an allowed abbreviation) are easier to make plain. Just parenthesising an entire expression makes no difference to its meaning:

    . di 42
    42
    
    . di (42)
    42
    

    But just as in elementary algebra, parentheses control which calculations are done first and so may make a difference.

      . di (1 + 2) / 3
    1
    
    . di 1 + 2 / 3
    1.6666667
    

    The precedence of operators is documented at

    . help operators
    

    Speaking for myself as a long-time Stata user and programmer, I have never tried to learn it and would not do well on a quiz about it. With very occasional exceptions, basic mathematical knowledge serves well enough, and as in high school mathematics, parenthesising aggressively is a better way to write clear and correct code than to rely on operator precedence.