Announcement

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

  • Possible to export variable labels as matrix of strings?

    I wish to export a matrix (or list) of variable labels from Stata to Tex. I though I'd do this by filling matrix with the labels, and then exporting via frmttable. However, I just realized that stata matrices cannot hold strings. Under any circumstances?

    Below is a reproducible example of what I'd *like* to do, except that the last mata st_matrix command doesn't run. In fact, normally this loop wouldn't involve mata at all, Sstats would be created as a stata matrix directly.

    Is there some way to export a series of 20-25 variable labels automatically, in a 1 column table, that doesn't involve matrices? Or can I somehow force a string-based matrix from mata? Thanks!

    Code:
    sysuse auto.dta, clear
    gl mylist mpg rep78 headroom trunk weight length turn displacement gear_ratio
    
    clear matrix
    local rows = 0
    foreach var of varlist $mylist  {
            local rows = `rows'+1
    }
    local cols 1
    mata Sstats = J(`rows',`cols',"")
    
    tokenize "$mylist"
    forval i = 1/`rows' {
        local x : variable label ``i''
        mata Sstats[`i',1]= "`x'"
        di "`x'"
    }
    mata Sstats
    mata st_matrix("Sstats",Sstats)

  • #2
    You can write directly to a file with file command. There is an example on how to do use that to create a codebook from a Stata dataset here: http://maartenbuis.nl/workshops/stata_l2/stata_l2.html . This should contain all you need and more to implement that strategy.
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Thank you very much! I didn't know about this command, very useful. However, I specifically wish to write into a .tex file, and ideally to automate some level of the .tex table formatting (though it would be possible to manually write out all the headings and footers myself). So this code works great for creating a .txt file, I understand from the link you sent that file doesn't have the ability to write to .tex files?

      Basically, this table can be viewed as a "result" within my paper, so I don't want to do any manual manipulation of it at all; it should go directly into a .tex table that can be compiled in latex. That's why I was originally trying to go w/ the matrix format, because with a matrix, I know how to do that. Any further thoughts? Am I missing some of the file capabilities? Thanks!

      Code:
      sysuse auto, clear
      
      tempname fh
      file open `fh' using "${coefs}Lasso/myfile.txt", write replace
      file write `fh' "Variables Chosen" _newline
      foreach v of varlist * {
      file write `fh' "`: variable label `v''" _newline
      }
      file close `fh'

      Comment


      • #4
        esttab (Stata Journal) can export variable labels, but you will need to look for a tex solution to select only the first column in the resulting table.

        Code:
        sysuse auto, clear
        local mylist mpg rep78 headroom trunk weight length turn displacement gear_ratio
        gen z=0
        qui reg z `mylist'
        esttab, plain lab noobs onecell drop(_cons)  tex

        Result:

        Code:
        . esttab, plain lab noobs onecell drop(_cons)  tex
        
        \begin{tabular}{l*{1}{c}}
                            &           .\\
                            &         b/t\\
        Mileage (mpg)       &         0 .\\
        Repair Record 1978  &         0 .\\
        Headroom (in.)      &         0 .\\
        Trunk space (cu. ft.)&         0 .\\
        Weight (lbs.)       &         0 .\\
        Length (in.)        &         0 .\\
        Turn Circle (ft.)   &         0 .\\
        Displacement (cu. in.)&         0 .\\
        Gear Ratio          &         0 .\\
        \end{tabular}
        Last edited by Andrew Musau; 17 Jul 2019, 18:22.

        Comment


        • #5
          I second the recommendation of -esttab-; that is what I usually use for putting Stata results in .tex files. With some tinkering, that could get what you want.

          However, when I want something very specific, I write the tex code using the -file- command. File can write to any extension (doesn't have to be .txt -- the file will be written in ASCII by default, so you can write .tex code directly into the file)
          Code:
          sysuse auto, clear
          tempname fh
          file open `fh' using "myfile.tex", write text replace
          file write `fh' "\begin{tabular}{l}" _n
          file write `fh' "Variables Chosen \\ \hline" _n
          foreach v of varlist * {
              file write `fh' "`: variable label `v'' \\" _newline
          }
          file write `fh' "\end{tabular}" _n
          file close `fh'
          and then in the tex document, write:
          Code:
          \begin{table}
          % other stuff, like captions
          \input{myfile.tex}
          \end{table}
          You could write the table environment in myfile.tex instead, but I like to leave it in the master .tex document so that I can easily change captions and other parameters without returning to Stata.

          Comment


          • #6
            Oh goodness, thank you! I just didn't realize that file could write to any file type at all, I feel like a dummy now. This is perfect, I'll do it like this. Thank again, Kye and Maarten! (And Andrew, this is a good point about the matrix, though of course I'd have to get rid of those zeros. Likely there's a work around though, so agreed that this could also work. Thanks!)

            Comment

            Working...
            X