Announcement

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

  • Need alternative to using the total command with matrix var = [e(b)]

    Hello,

    I provided some code below, but briefly explain my issue first.

    I am creating tables in Stata for Excel using the putexcel command. I have a datafile of adult prisons in the 50 US states (1,152 observations total). In this datafile, some states have 20 prisons while other states have 1 prison. The variable I am working with, labeled f50_i, measures prison populations. I am trying to store and then export the sum of a state's prison population to cells in my state-specific Excel tables. In the states with 20 prisons, this would be the sum of all prisoners across the 20 prisons. In the states with 1 prison, it would be the number of prisoners in the one facility in that state.

    I could just create a state-specific sum using bysort state: egen popcount = sum(f50_i), but I am trying to do this across multiple tables with different conditioning statements (e.g., if var1!=. & var2==4). This changes the number of prisons (and, therefore, prisoners) in the state across commands. To account for the changing sample of prisons within states across conditioning statements, I used the total command and stored the prison population value using matrix var = [e(b)]. See code below. The problem is that this command is not working when there are zero or one prisons in the state after applying the conditioning statements. When there are zero prisons, I get the error: no observations. When a state has one prison, I get the error: estimates post: matrix has missing values. In both cases, the loop does not work. In the cases with zero prisons (and, therefore, a value of zero on f50_i), I would still like the table to have the value 0 prisoners. Again, my code works fine when there are 2 or more prisons. Could someone suggest one or more alternatives to total that will supply a value in these cases that I can store and export to Excel?

    levelsof state, local(levels)
    foreach i of local levels {

    total f50_i [iw=wgt_long] if state=="`i'"
    matrix authorityN = [e(b)]
    total f50_i [iw=wgt_long] if state=="`i'" & rc_budget_auth_i==1
    matrix authority1a= [e(b)]
    total f50_i [iw=wgt_long] if state=="`i'" & rc_personnel_auth_i==1
    matrix authority2a= [e(b)]
    total f50_i [iw=wgt_long] if state=="`i'" & rc_policies_auth_i==1
    matrix authority3a= [e(b)]

    matrix authoritytot = [(authorityN) \. \ (authority1a) \ (authority2a) \ (authority3a)]

    putexcel set "`i'/Table5b", sheet("Table 5b") modify
    putexcel B12 = matrix(authoritytot)
    }


    Thank you,
    Tom S

  • #2
    Joseph Coveney sorry to bother you, but you helped me with something similar in the past. I was wondering if you could help me here as well. Thank you.

    Comment


    • #3
      Hi Tom,

      I am having the same issue that you described here (problems when there are no observations) and came across your post. Did you ever manage to get around this issue? Any help you can give would be amazing!

      Thanks,
      Angela

      Comment


      • #4
        Angela Kinnell yes it was pretty simple. I just used the sum command with r(sum):

        foreach folder in local folderpath {
        foreach i of local levels {
        sum f50_i [w=wgt_long] if state=="`i'", detail
        matrix authorityN = r(sum)
        sum f50_i [w=wgt_long] if state=="`i'" & rc_budget_auth_i==1, detail
        matrix authority1a= r(sum)
        sum f50_i [w=wgt_long] if state=="`i'" & rc_personnel_auth_i==1, detail
        matrix authority2a= r(sum)
        sum f50_i [w=wgt_long] if state=="`i'" & rc_policies_auth_i==1, detail
        matrix authority3a= r(sum)
        sum f50_i [w=wgt_long] if state=="`i'" & rc_budget_oper_resp_i==1, detail
        matrix authority4a= r(sum)
        sum f50_i [w=wgt_long] if state=="`i'" & rc_personnel_oper_resp_i==1, detail
        matrix authority5a= r(sum)
        sum f50_i [w=wgt_long] if state=="`i'" & rc_policies_oper_resp_i==1, detail
        matrix authority6a= r(sum)
        sum f50_i [w=wgt_long] if state=="`i'" & c17c_i==1, detail
        matrix authority7a= r(sum)

        matrix authoritytota = [(authorityN) \. \ (authority1a) \ (authority2a) \ (authority3a) \ . \ (authority4a) \ (authority5a)\ (authority6a)\ (authority7a)]
        mat li authoritytota

        mata: st_matrix("authority1b", st_matrix("authority1a") :/ st_matrix("authorityN"))
        mata: st_matrix("authority2b", st_matrix("authority2a") :/ st_matrix("authorityN"))
        mata: st_matrix("authority3b", st_matrix("authority3a") :/ st_matrix("authorityN"))
        mata: st_matrix("authority4b", st_matrix("authority4a") :/ st_matrix("authorityN"))
        mata: st_matrix("authority5b", st_matrix("authority5a") :/ st_matrix("authorityN"))
        mata: st_matrix("authority6b", st_matrix("authority6a") :/ st_matrix("authorityN"))
        mata: st_matrix("authority7b", st_matrix("authority7a") :/ st_matrix("authorityN"))

        matrix authoritytotb = [1.0000 \ . \ (authority1b) \ (authority2b) \ (authority3b) \ . \ (authority4b) \ (authority5b) \ (authority6b) \ (authority7b)]
        mat li authoritytotb

        putexcel set "`i'/Table4b", sheet("Table 4b") modify
        putexcel B12 = matrix(authoritytota)
        putexcel C12 = matrix(authoritytotb)
        }
        }

        Comment

        Working...
        X