Announcement

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

  • Export tabulation to latex

    Hi Statalist,

    I would like to export tabulations for categorical variables to Latex.

    Here is one categorical variable I am using:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float social18_beer_cat
    0
    3
    2
    2
    0
    3
    0
    0
    1
    end
    I would like to export the tabulation of "social18_beer_cat" so that I have the 1) frequency and 2) percentages.

    I did some troubleshooting but cannot find the solution.
    I tried to use a loop since I have several categorical variable which follows the same structure.
    Code:
    global social "social18_one_cat social18_two_cat social18_beer_cat social18_four_cat social18_five_cat social18_six_cat"
    
    unab vars : $social
    local n = `: word count `vars''
    local i = 1
    foreach v of varlist $social {
    matrix  `v' = J(4,2,.)
    tab `v', matcell(freq)
    local j = 1
    matrix  `v'[1,`j'] = r(freq)
    local ++j
    matrix  `v'[1,`j'] = r(percent)
    local ++j
    
    mata: `v' = st_matrix("`v'")
    mmat2tex `v' using `v'.tex, replace fmt("%12.3f" "%12.3f")
    }
    Thank you so much!

  • #2
    See #13 onwards https://www.statalist.org/forums/for...-test-to-latex

    Comment


    • #3
      Hi Andrew,
      Thank you for the reply. I looked through the codes, but still couldn't figure out.
      Is there a way to export only the frequency and percentages to latex?

      Thank you!

      Comment


      • #4
        Install estout from SSC.

        Code:
        ssc install estout
        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input float social18_beer_cat
        0
        3
        2
        2
        0
        3
        0
        0
        1
        end
        
        estpost tab social18_beer_cat
        esttab, cell("b pct(fmt(a))")  collab("Freq." "Percent") noobs nonumb nomtitle
        esttab, cell("b pct(fmt(a))")  collab("Freq." "Percent")  noobs nonumb nomtitle tex
        Res.:

        Code:
        . esttab, cell("b pct(fmt(a))")  collab("Freq." "Percent") noobs nonumb nomtitle
        
        --------------------------------------
                            Freq.      Percent
        --------------------------------------
        0                       4        44.44
        1                       1        11.11
        2                       2        22.22
        3                       2        22.22
        Total                   9          100
        --------------------------------------
        
        . esttab, cell("b pct(fmt(a))")  collab("Freq." "Percent")  noobs nonumb nomtitle tex
        
        {
        \def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
        \begin{tabular}{l*{1}{cc}}
        \hline\hline
                    &       Freq.&     Percent\\
        \hline
        0           &           4&       44.44\\
        1           &           1&       11.11\\
        2           &           2&       22.22\\
        3           &           2&       22.22\\
        Total       &           9&         100\\
        \hline\hline
        \end{tabular}
        }
        
        . 
        
        .
        Last edited by Andrew Musau; 11 May 2021, 14:28.

        Comment


        • #5
          x
          Last edited by Denat Ephrem Negatu; 03 Apr 2023, 16:41.

          Comment


          • #6
            I have two questions related this:

            1. if i have two tabulate commands as follows:
            eststo a: estpost tab catagorical_1 if catagorical_2 == 1
            eststo a: estpost tab catagorical_1

            is there a way for me to report the frequency and percentage from "a" but only the percentage from "b"

            2. If I can only the same kind of paramenteres for instance "pct" from the two tabs, is there a way to edit the titel from "a" differently from "b".


            the current code I have looks as follows but it only reports percent and I'm unable to rename differently:

            esttab a b using "$path/table.tex", cells("pct(fmt(2))") nonumber nomtitle nodepvars noobs collab("\% within") keep(1 2 5 20 22) replace

            Comment


            • #7
              Please provide a reproducible example.

              Comment


              • #8
                Originally posted by Andrew Musau View Post
                Please provide a reproducible example.
                yes, here is an example

                eststo a: estpost tab rep78 if foreign == 1
                eststo b: estpost tab rep78

                esttab a b using _test.tex, cells("pct(fmt(2))") nonumber nomtitle nodepvars noobs collab("\% within" "\%total") keep(3 4 5) replace

                right now, the title "\%within" is twice



                Comment


                • #9
                  Originally posted by Andrew Musau View Post
                  Please provide a reproducible example.
                  sysuse auto.dta , clear

                  yes, here is an example

                  eststo a: estpost tab rep78 if foreign == 1
                  eststo b: estpost tab rep78

                  esttab a b using _test.tex, cells("pct(fmt(2))") nonumber nomtitle nodepvars noobs collab("\% within" "\%total") keep(3 4 5) replace

                  right now, the title "\%within" is twice



                  Comment


                  • #10
                    Code:
                    sysuse auto.dta , clear
                    eststo a: estpost tab rep78 if foreign == 1
                    eststo b: estpost tab rep78
                    esttab a a b, cells("b(pattern(1 0 0)) pct(pattern(0 1 1)fmt(2))") ///
                    nonumber nomtitle nodepvars noobs collab(none) keep(3 4 5) ///
                    replace mlab("Freq 1" "Percent 1" "Percent 2", lhs(Category))



                    Code:
                    . esttab a a b, cells("b(pattern(1 0 0)) pct(pattern(0 1 1)fmt(2))") ///
                    > nonumber nomtitle nodepvars noobs collab(none) keep(3 4 5) ///
                    > replace mlab("Freq 1" "Percent 1" "Percent 2", lhs(Category))
                    
                    ---------------------------------------------------
                    Category           Freq 1    Percent 1    Percent 2
                    ---------------------------------------------------
                    3                       3        14.29        43.48
                    4                       9        42.86        26.09
                    5                       9        42.86        15.94
                    ---------------------------------------------------

                    Comment


                    • #11
                      That worked! Thank you so much!

                      Comment


                      • #12
                        Hi again, Im trying to export stata tabulate to latex. Im creating two panels but i don't want to have title of the second variable in panel b. I have tried nomtitle but it doesn't seem to work. Any suggestion?

                        Here is an example code:

                        sysuse auto.dta , clear

                        estpost tab rep78 headroom

                        esttab using "$path/_table.tex", ///
                        nolines ///
                        prehead("\begin{tabular}{l*{9}{c}} \hline\hline") ///
                        posthead("\hline \multicolumn{8}{l}{\textbf{Panel A: \% of Total}} \\ \hline") ///
                        fragment ///
                        cell("pct(fmt(%2.0f))") unstack noobs replace nonum collabels(none) eqlabels(, lhs("`:variable label rep78'")) mtitles("`:variable label make'")

                        //Panel B

                        esttab using "$path/_table.tex", ///
                        prehead("") ///
                        posthead("\hline \multicolumn{8}{l}{\textbf{Panel B: \% within Constraint}} \\ \hline") ///
                        fragment ///
                        append ///
                        cell("colpct(fmt(%2.0f))") collabels(none) eqlabels(, lhs("`:variable label rep78'")) ///
                        prefoot("\hline") ///
                        postfoot("\hline \end{tabular}") ///
                        unstack noobs nonum nomtitles

                        Comment


                        • #13
                          You are appending tables, so those are two tables and the command is designed to treat each appended table as a separate table. You may want to write to the author (Ben Jann) and ask him to include an option that treats the appended table or tables as part of the initial table. For now, I would just manually delete the titles. As I stated in this thread, the problem is not interesting enough for me to explore.

                          Comment


                          • #14
                            I see. Thank you. It is just a little strange as I am able to do this when exporting regression tables but not tabulation. I will try to see if I can reach the author.

                            Comment


                            • #15
                              asdocx offers a variety of tools to export tables created from tabulation of categorical variables. The tables can be exported to Word, Excel, LaTeX, or HTML. Here is a few links to explore:

                              tabmany – Table of multiple coded answers

                              mrtab – One- and two-way tables of multiple responses

                              tabcount – tabulates frequencies for up to 7 variables

                              tab3way – Three way table of frequencies and percentages

                              missings – Various utilities for managing missing values

                              tabulate, tab1, tab2

                              pctab – report percentages in cross-tabulations

                              crosstab - Tabulation with weighted means, percentages, etc.
                              Regards
                              --------------------------------------------------
                              Attaullah Shah, PhD.
                              Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
                              FinTechProfessor.com
                              https://asdocx.com
                              Check out my asdoc program, which sends outputs to MS Word.
                              For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

                              Comment

                              Working...
                              X