Announcement

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

  • putexcel with Matrix

    Hi all

    I want to create a table in excel using the matrix command. I wrote:
    Code:
    mkmat year  WE1 WE2  if dupYrUr<2 & urb==1, matrix(urban)
    matrix list urban
    putexcel  A4=matrix(urban), names nformat(number_d2)
    The resulting table in excel:
    year WE1 WE2
    r1 1997 0.78 0.92
    r2 2001 0.72 0.88
    r3 2002 0.71 0.86
    Is there a way to get rid of the column with r1, r2, r3? Better, is it possible to export the table with r1,r2,r3 column?
    Thanks.

  • #2
    Regarding row names and column names, the output of help putexcel tells us there are three "names" options for the putexcel matrix command:
    Code:
    names     also write row names and column names for
                matrix matname; may not be combined with
                rownames or colnames
    rownames  also write matrix row names for matrix
                matname; may not be combined with names
                or colnames
    colnames  also write matrix column names for matrix
                matname; may not be combined with names
                or rownames
    Perhaps this example will start you in a useful direction.
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int year float(WE1 WE2)
    1997 .78 .92
    2001 .72 .88
    2002 .71 .86
    end
    
    generate Year = string(year)
    mkmat  WE1 WE2, matrix(urban) rownames(Year)
    matrix list urban
    matrix urban = urban'
    matrix list urban
    putexcel set "~/Downloads/pe.xlsx", replace
    putexcel  A4=matrix(urban), colnames nformat(number_d2)
    Code:
    . generate Year = string(year)
    
    . mkmat  WE1 WE2, matrix(urban) rownames(Year)
    
    . matrix list urban
    
    urban[3,2]
                WE1        WE2
    1997  .77999997  .92000002
    2001  .72000003        .88
    2002  .70999998  .86000001
    
    . matrix urban = urban'
    
    . matrix list urban
    
    urban[2,3]
              1997       2001       2002
    WE1  .77999997  .72000003  .70999998
    WE2  .92000002        .88  .86000001
    
    . putexcel set "~/Downloads/pe.xlsx", replace
    Note: file will be replaced when the first putexcel command is issued
    
    . putexcel  A4=matrix(urban), colnames nformat(number_d2)
    file /Users/lisowskiw/Downloads/pe.xlsx saved
    Click image for larger version

Name:	pd.png
Views:	1
Size:	31.9 KB
ID:	1555828

    Last edited by William Lisowski; 28 May 2020, 18:55.

    Comment


    • #3
      Thanks William. This is very helpful.

      Comment

      Working...
      X