Announcement

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

  • Creating fractions with dummy variables

    Hi!

    I have a question on how to create fractions when using dummy variables. I would like to calculate a fraction, by district, consisting of the share of the dummy variable equal to 1 of the total (dummy equal to either 1 or 0). However, when I try to do this I end up with only 1's and a lot of missing values as a result of trying to divide 1/0. Is there any way to 'count' the total number of observations so that I can get the actual fraction of 1's in relation to the total amount by district?

    Thank you in advance!

  • #2
    We can't explain what went wrong if you don't explain exactly what you did.

    This may help. The proportion of foreign cars is just the mean over foreign which is 0 or 1.


    Code:
    sysuse auto, clear
    
    egen pforeign = mean(foreign), by(rep78)
     
    tabdisp rep78, c(pforeign) format(%3.2f)
    
    ----------------------
    Repair    |
    Record    |
    1978      |   pforeign
    ----------+-----------
            1 |       0.00
            2 |       0.00
            3 |       0.10
            4 |       0.50
            5 |       0.82
            . |       0.20
    ----------------------
    Note that this is legal too:


    Code:
    egen pforeign = mean(1 - foreign), by(rep78)
    as is this

    Code:
    egen pforeign = mean(!foreign), by(rep78)

    Comment

    Working...
    X