Announcement

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

  • formatting a matrix for use in putexcel

    Dear all,
    In Stata 14, I'm using a loop to build a matrix containing summary statistics for several variables, which is then written to an Excel spreadsheet using putexcel. I'm trying to format each number to look the way I want it to once it is written to the Excel file. The problem is that whenever I have a number large enough to have a comma, only the digits prior to the comma are written to the Excel file.

    In the code below, the line
    Code:
    local treatmean: display %9.2fc `r(mean)'
    is supposed to take the mean of the variable and format it to display correctly. The macro `treatmean' is then added to a matrix which is written to the Excel file.
    If I type
    Code:
    di "`treatmean'"
    right after the
    Code:
    local treatmean: display %9.2fc `r(mean)'
    command, the macro displays correctly. However, if I type
    Code:
    di `treatmean'
    (no quotation marks) the macro displays with a space instead of the comma. This leads me to believe that if I could fill in a matrix cell with "`treatmean'" rather than `treatmean', the resulting number would display correctly. But I cannot do that because I get a type mismatch.

    The code is below. It is heavily commented because I was sharing the code with someone else who is not very familiar with Stata.

    Code:
    local variables gastoaliment_percap FCS goodFCS
    
    tokenize `variables' /* takes the first element of `variables' and assigns local 
    macro `1' to it, assigns `2' to y, etc.*/
    local j: list sizeof variables
    
    di `j'
    
    matrix stats = J(`j', 6, .) /* blank matrix with `j' rows and 6 columns */
    
    forvalues i = 1/`j' {
    local l: var lab ``i'' /* variable label will go in our results matrix */
    *summary stats
    su ``i'' if treat2014 == 1 /* this becomes "su `1'" the first time through the loop. `1' gets 
    replaced with x because of the "tokenize" command above */
    local treatmean: display %9.2fc `r(mean)' /* display %9.2fc changes each number into a number
    that has a maximum of 9 columns, has commas separating 1,000s, and two decimal
    places */
    local treatsd: display %9.2fc `r(sd)'
    su ``i'' if treat2014 == 0
    local controlmean: display %9.2fc `r(mean)'
    local controlsd: display %9.2fc `r(sd)'
    di `treatmean'
    * difference in means
    qui reg ``i'' treat2014, cluster(Comunidad2)
    local diff: display %9.2fc _b[treat]
    local pvalue: display %9.2fc ttail(`e(df_r)', abs(_b[treat]/_se[treat]))*2 
    /* the ttail command returns the probability that T > t. 
    We want the probability that T > abs(t), 
    that is, the pvalue for a two-tailed test. So we have to multiple the 
    p-value by 2. Also, Stata uses the number of groups - 1 as the degrees of 
    freedom when you use a cluster-robust covariance matrix */ 
    
    * fill in the matrix
    matrix stats[`i',1] = `treatmean' /* mean */
    matrix stats[`i',2] = `treatsd' /* st. dev. */
    matrix stats[`i',3] = `controlmean' /* mean */
    matrix stats[`i',4] = `controlsd' /* st. dev. */
    matrix stats[`i',5] = `diff' /* difference in means */
    matrix stats[`i',6] = `pvalue' /* p-value goes below the mean difference */
    local ii = `i' + 11 // 11 rows of summary stats already created in earlier file
    
    putexcel A`ii' = ("``i''") B`ii' = ("`l'") I`ii' = (`e(N)') ///
    using guatemala_MDE, sheet("summary stats") modify
    }
    putexcel C12= matrix(stats) using guatemala_MDE, ///
    sheet("summary stats") modify

  • #2
    I think the problem is that a Stata matrix stores numbers, not character strings, and it will never include the comma. I don't see any way to associate a format with a particular column or row or cell of a matrix, but I'm also not that experienced with them.

    You might look at help matrix list and try matrix list before your putexcel to see exactly what you have in the matrix. Also, help putexcel suggests that you may want to use the keepcellformat option to preserve the formats already assigned to the cells in the worksheet.

    Comment

    Working...
    X