Announcement

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

  • Powers of "tempvar"

    Dear users,
    I have noticed that if one declares a tempvar, say

    tempvar october

    and later on assign a variable which can take positive or negative values,

    gen `october' = 2*runiform() -1

    when one does

    gen october2 = ``october'' ^2

    the result is the square of the positive numbers and, worryingly, the "negative" of the square of the negative numbers (that is, the sing is preserved). Thus, if `october' = -2, then ``october''^2 = -4. To get the right answer, one seems to have to do

    gen october2 = (``october'')^2

    So, my question is, has anyone experience this before? Is this a well known Stata trait??????

    Thank you.



  • #2
    http://www.stata.com/support/faqs/pr...ower-operator/
    -------------------------------------------
    Richard Williams, Notre Dame Dept of Sociology
    StataNow Version: 19.5 MP (2 processor)

    EMAIL: [email protected]
    WWW: https://www3.nd.edu/~rwilliam

    Comment


    • #3
      Your conclusion is incorrect—there is nothing wrong with temporary variables, and indeed, they are the same as any other variable except that they are named automatically by Stata and dropped when your program or do-file ends. Part of the reason for your confusion may be that you are not using them properly. The statement
      tempvar october
      assigns an (automatically generated) variable name to the local macro october. To refer to it, you expand the macro with the local macro expansion characters ` and '. So, your statement
      gen `october' = 2*runiform() - 1
      is correct, but your statement
      gen october2 = ``october'' ^ 2
      is not. Instead, it should be
      gen october2 = `october' ^ 2
      Understand what's going on here. Your temporary variable may have a name that looks like __000000 (just an example). So, when you write
      gen october2 = `october' ^ 2
      you are essentially writing
      gen october2 = __000000 ^ 2
      However, when you write
      gen october2 = ``october'' ^ 2
      you are instead writing
      gen october2 = `__000000' ^ 2
      which will result in a syntax error unless the local macro __000000 exists, in which case it will create the square of whatever variable is stored in the local macro __000000.

      Now, since the square of a real number is non-negative, even if you managed to create a temporary variable (macro) named __000000 (i.e., `october'), the statement
      gen october2 = ``october'' ^ 2
      would still yield a positive result. Thus, I believe what you did is not what you show in your post, but rather something like this:
      . loc foo -1
      . di `foo' ^ 2
      -1
      . di (`foo') ^ 2
      1
      But, there's no mystery here; since `foo' expands to -1, the first statement is equal to
      di -1^2
      and the second is equal to
      di (-1)^2
      which behave according to Stata's order of operations.

      Before doing more with macros (including those generated by tempvar, tempfile, etc.), I would suggest reading the documentation on macro definition and manipulation.
      Last edited by Phil Schumm; 04 Jul 2014, 06:32.

      Comment


      • #4
        Also, is that what you really typed? I get an error with your code, which seems to have overkill with apostrophes. I don't get an error (or negative values) when I just use one apostrophe on each side of October, e.g.

        Code:
        . clear all
        
        . set seed 123
        
        . set obs 100
        obs was 0, now 100
        
        . tempvar october
        
        . gen `october' = 2*runiform() -1
        
        . sum
        
            Variable |       Obs        Mean    Std. Dev.       Min        Max
        -------------+--------------------------------------------------------
            __000001 |       100   -.0282718    .5965548  -.9999809   .9851494
        
        . gen october2 = ``october'' ^2
        ^2 invalid name
        r(198);
        
        . gen october2 = `october' ^2
        
        . sum
        
            Variable |       Obs        Mean    Std. Dev.       Min        Max
        -------------+--------------------------------------------------------
            __000001 |       100   -.0282718    .5965548  -.9999809   .9851494
            october2 |       100    .3531181     .331815    .000124   .9999617
        Maybe you did something more like this. This behavior is explained in the link I gave earlier.

        Code:
        . clear all
        
        . local october = -4
        
        . di `october' ^2
        -16
        
        . di (`october') ^2
        16
        -------------------------------------------
        Richard Williams, Notre Dame Dept of Sociology
        StataNow Version: 19.5 MP (2 processor)

        EMAIL: [email protected]
        WWW: https://www3.nd.edu/~rwilliam

        Comment


        • #5
          Dear Richard and Phil,
          Thank you for your prompt response. Truly my post was a bit misleading. Phil's clarification regarding tempvars is very useful. However, as Richard has spotted, I was in fact referring to the 1999 "power of macro" issue that he mentioned in both his posts -and so his answer solves my problem!

          ...yet, the fact that

          . di `october' ^2 = -16

          means trouble for those of us who are used to programming other packages (Ox and GAUSS). I only spotted this because I was computing a variance via the delta method and one of the "positive" terms turned out negative!!.

          Once again, thank you both for your kind help.
          Best regards,
          Eduardo.

          Comment

          Working...
          X