Announcement

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

  • Output from summary statistics partially exported to excel

    Hello everyone,

    Please can you help me. I have a dataset that is ordered by year and bank. The bank variable has 20 banks observed over 4 years. I would like to obtain some descriptive statistics and export the tables to excel for my variables such as default_rate and credit_score . That is the (n mean sd min max) by year and bank. However, my amateur's commands seem to be horribly wrong. My main worry is that, when i export the results using _putexcel the output I get is just for the last bank repeated 20 times and not the table results for each bank. How can I fix this? An example code of what I am doing using webuse data is as follows?


    Code:
    webuse productivity, clear
    
    drop if year > 1976
    
    * Create an empty matrix to store results
    matrix result = J(1, 5, .)
    
    *Loop through each state and calculate summary statistics
    forval i = 1/48 {
        bysort state year: tabstat unemp gsp , statistics(n mean sd min max) save
            mat mat`i' = r(StatTotal)'
        
     *Append the results to the main matrix
        matrix result = result \ mat`i'
    }
    
    
    matrix colnames result = "n" "mean" "sd" "min" "max"
    
    *Export the results to Excel using putexcel
    
    putexcel set "path\to\my\output_file.xlsx", modify sheet(Sheet1)
    putexcel A1 = matrix(result)

  • #2
    You want the statistics by state and not state and year. For example, the mean is constant for a given state-year statistic and the SD is zero (no variance). Look at the levelsof command for a way to select distinct levels of a variable.

    Code:
    help levelsof
    I also merge 2 letter state abbreviations, assuming that you have a string to identify these in your dataset. Then one can make sense of the final table. Also, there are canned programs for creating and exporting these kinds of summary statistics, see

    Code:
    help collect
    and

    Code:
    ssc install estout, replace
    help estpost
    Search the forum for example codes.

    Code:
    webuse census, clear
    keep state2
    keep if _n<49
    tempfile 2lsabb
    gen state=_n
    save `2lsabb'
    
    webuse productivity, clear
    clear matrix
    drop if year > 1976
    merge m:1 state using `2lsabb', nogen
    
    *Loop through each state and calculate summary statistics
    assert int(state[1])
    qui levelsof state, local(statecodes) clean
    foreach num of numlist `statecodes'{
        tabstat unemp gsp if state==`num', statistics(n mean sd min max) save
        mat mat`num' = r(StatTotal)'
        qui levelsof state2 if state==`num', local(stateabb) clean
        mat rowname mat`num'= "unemp_`stateabb'" "gsp_`stateabb'"
        *Append the results to the main matrix
        matrix result = nullmat(result) \ mat`num'
    }
    matrix colnames result = "n" "mean" "sd" "min" "max"
    mat l result
    
    *Export the results to Excel using putexcel
    putexcel set "path\to\my\output_file.xlsx", modify sheet(Sheet1)
    putexcel A1 = matrix(result)
    Res.:

    Code:
    . mat l result
    
    result[96,5]
                      n       mean         sd        min        max
    unemp_AL          7  5.4999999  1.3203535  3.9000001  7.6999998
      gsp_AL          7   10.37788  .08293673  10.254778  10.484697
    unemp_AK          7        6.4  3.1775253  4.0999999         12
      gsp_AK          7  10.065474  .11430758   9.867238  10.167427
    unemp_AZ          7  5.7857143  1.8933467  4.0999999        9.5
      gsp_AZ          7  9.7993156  .10016474  9.6416035  9.9172421
    unemp_AR          7  8.1999999  1.1030261          7  9.8999996
      gsp_AR          7  12.574227  .07144776  12.483451  12.676577
    unemp_CA          7  4.5714286  1.3199928  3.4000001  6.9000001
      gsp_CA          7  10.335673  .11670012  10.153818  10.469455
    unemp_CO          7  7.5999999   1.706849  5.5999999        9.5
      gsp_CO          7  10.605382   .0354268  10.558803  10.649156
    unemp_CT          7   6.342857  2.0998865  4.5999999  9.6999998
      gsp_CT          7    8.95164  .07032712  8.8338995  9.0599823
    unemp_DE          7  6.3285715  2.6518636  4.3000002         11
      gsp_DE          7  11.332381  .11559159  11.151109  11.431314
    unemp_FL          7  5.3857144   2.064323  3.9000001  8.6000004
      gsp_FL          7  10.812492  .08543073  10.671394  10.905387
    unemp_GA          7  5.9714285  .27516231  5.5999999  6.3000002
      gsp_GA          7  9.0243993  .11610657  8.8783579   9.180809
    unemp_HI          7  5.2142856  1.1710801  4.0999999  7.0999999
      gsp_HI          7   11.95859  .04557116  11.889936  12.002272
    unemp_ID          7  5.7142857  1.4599414  4.1999998  8.6000004
      gsp_ID          7  11.034459  .05856203  10.946746  11.105469
    unemp_IL          7  3.6714286  .55291434  2.9000001  4.3000002
      gsp_IL          7  10.384759  .08046389  10.280999  10.484362
    unemp_IN          7  4.2428571  .81005589  3.0999999        5.5
      gsp_IN          7  10.262699  .05523555  10.177742  10.330551
    unemp_IA          7  5.3000001  .99331099  4.4000001  7.3000002
      gsp_IA          7  10.486934  .06973373  10.379163  10.574849
    unemp_KS          7  6.6571428  .48941175          6  7.4000001
      gsp_KS          7  11.084953  .02865265  11.045574  11.118979
    unemp_KY          7  7.3999999  1.5726835  5.6999998         10
      gsp_KY          7  9.1708531  .06266101  9.0874949  9.2643547
    unemp_LA          7  4.7285714  1.5217472        3.3  6.9000001
      gsp_LA          7  10.751908  .05302006  10.668281  10.804218
    unemp_ME          7  7.4285714  2.1359731  4.5999999         11
      gsp_ME          7  11.119507  .03329824  11.073319  11.168687
    unemp_MD          7  8.3142856  2.3961278  5.8000002         13
      gsp_MD          7  11.633401  .06521242  11.534413  11.719867
    unemp_MA          7  4.7714287  .77398168  4.1999998  5.9000001
      gsp_MA          7  10.726552  .06233599  10.639071  10.795383
    unemp_MI          7  5.1571429    1.69986  3.5999999  8.3000002
      gsp_MI          7  9.8856668  .08502496  9.7588081  10.011535
    unemp_MN          7  4.7142856  1.1393398        3.3  6.1999998
      gsp_MN          7  10.899246  .04655402   10.82369  10.954012
    unemp_MS          7  6.2142857   .3670993        5.5  6.6999998
      gsp_MS          7   9.073132  .07854078  8.9655905  9.1615705
    unemp_MO          7  3.5285714  .37289085  3.0999999  4.1999998
      gsp_MO          7   9.784286  .06407418  9.6845226   9.862092
    unemp_MT          7  7.4714285  1.4044148  5.9000001  9.6999998
      gsp_MT          7  9.0429425  .10092914  8.9029999   9.181941
    unemp_NE          7  5.0571428  2.0139987        3.3          9
      gsp_NE          7  8.9448425  .07695862  8.8322954  9.0442858
    unemp_NV          7  6.9428571  2.1923026  4.5999999         10
      gsp_NV          7  11.432315  .04210003  11.366349   11.49116
    unemp_NH          7  7.0000001  1.7738847  5.6999998         10
      gsp_NH          7   9.577084  .08219213  9.4600868  9.6760845
    unemp_NJ          7          7  2.0330601        4.5         10
      gsp_NJ          7  12.420601  .02310778  12.398604  12.462644
    unemp_NM          7  5.1285715  1.7471065        3.5  8.6000004
      gsp_NM          7  10.909988  .07977198  10.779935  10.993093
    unemp_NY          7        4.3  .89069267          3  5.3000002
      gsp_NY          7  8.9091218  .12697098  8.7035065  9.0200272
    unemp_NC          7  6.2285716  1.6948101  4.3000002  9.1000004
      gsp_NC          7  11.770115  .04801668  11.705964  11.832368
    unemp_ND          7  5.0285714  1.0656989  4.1999998  7.1999998
      gsp_ND          7  10.475027  .04106221   10.41217  10.528784
    unemp_OH          7        7.4  2.1118712  5.3000002         11
      gsp_OH          7  10.165617  .10065986  10.014582  10.294144
    unemp_OK          7  5.9142858  1.5312616        4.5  8.3000002
      gsp_OK          7  11.823694  .03756337  11.775036   11.87039
    unemp_OR          7  7.3000001  1.8636882  5.1999998         11
      gsp_OR          7  9.2056261  .03384839  9.1519699    9.25875
    unemp_PA          7  5.4714286  1.7518697        3.7  8.6999998
      gsp_PA          7  10.074234  .08408156  9.9486523  10.177476
    unemp_RI          7  3.5142857  .18644548        3.3        3.7
      gsp_RI          7  8.7980535  .07845278  8.6765871  8.8754272
    unemp_SC          7  4.9428572   1.783122          3  8.3000002
      gsp_SC          7  10.613365  .09588711  10.456482  10.721261
    unemp_SD          7  4.7571429  .67788174  3.9000001  5.6999998
      gsp_SD          7  12.104642  .09021583  11.988675  12.233406
    unemp_TN          7  6.0571428  .31547402  5.6999998        6.5
      gsp_TN          7  9.4345623   .1029173  9.2826614  9.5690632
    unemp_TX          7  6.9714285  1.5976171  4.9000001  9.3999996
      gsp_TX          7  8.4315256  .03570421    8.37885  8.4765797
    unemp_UT          7  4.3571429   1.246137  3.4000001  6.4000001
      gsp_UT          7  10.906731  .08103475  10.784545  11.004131
    unemp_VT          7  8.8142857  1.0237653  7.1999998         10
      gsp_VT          7  10.671127  .08736807  10.570445  10.793373
    unemp_VA          7  6.6714285  .99618322  5.6999998        8.5
      gsp_VA          7  9.8584115  .03927695  9.8016205  9.9179821
    unemp_WA          7  4.8285714  1.0672618  3.9000001  6.9000001
      gsp_WA          7  10.816234  .06349972  10.721085  10.894013
    unemp_WV          7  4.0571428  .39520941        3.5        4.5
      gsp_WV          7  9.0222528     .07526  8.9231911  9.1112928

    Comment


    • #3
      I concur, you probably don't want this by state and year since that specifies exactly one observation. Definitely seems like you are doing things the hard way. Here is an implementation using the built in -collect- command set.

      Code:
      webuse productivity, clear
      
      collect: table state, stat(n unemp gsp) stat(mean unemp gsp) stat(sd unemp gsp) ///
                            stat(min unemp gsp) stat(max unemp gsp)
      collect export test.xlsx, replace

      Comment


      • #4
        Ohhh thank you very much Andrew and Daniel. This is exactly what I needed and have been failing to do for hours on end.

        Comment

        Working...
        X