Search code examples
stata

How to save the results of calculations in a variable


I have multiple lines of proportion calculations, like this:

proportion DIAEDUC

This command gives me the following result:

Proportion estimation             Number of obs   =     10,802

--------------------------------------------------------------
             |                                   Logit
             | Proportion   Std. Err.     [95% Conf. Interval]
-------------+------------------------------------------------
DIAEDUC      |
           0 |    .972774   .0025372      .9673336    .9773295
           1 |    .027226   .0025372      .0226705    .0326664
--------------------------------------------------------------

Is there a way to save the value .027226 in a variable?


Solution

  • The results are returned to the e(b) matrix:

    sysuse auto, clear
    
    proportion foreign
    
    Proportion estimation             Number of obs   =         74
    
    --------------------------------------------------------------
                 | Proportion   Std. Err.     [95% Conf. Interval]
    -------------+------------------------------------------------
    foreign      |
        Domestic |   .7027027   .0534958      .5865827    .7974684
         Foreign |   .2972973   .0534958      .2025316    .4134173
    --------------------------------------------------------------
    
    matrix list e(b)
    
    e(b)[1,2]
         foreign:  foreign:
        Domestic   Foreign
    y1  .7027027  .2972973
    

    You can then store the result of interest in a variable as follows:

    matrix A = e(b)
    generate result = A[1,2]
    
    list result in 1
    
         +----------+
         |   result |
         |----------|
      1. | .2972973 |
         +----------+