Search code examples
special-charactersstata

How to generate new variable from stored results: r(mean)?


I am trying to generate a new variable using the stored result r(mean) from the command sum.

I have a continuous variable 'age'. So, sum age g age0=age–r(mean)

The problem is that this gives the error unknown function age–r() r(133);.


Solution

  • Works for me:

    . sysuse auto, clear
    (1978 Automobile Data)
    
    . su mpg
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
             mpg |         74     21.2973    5.785503         12         41
    
    . gen mpg0 = mpg - r(mean)
    
    . su mpg0
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
            mpg0 |         74   -4.03e-08    5.785503  -9.297297    19.7027
    

    Watch for strange characters, i.e. that the minus sign really is a hyphen [sic].