Announcement

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

  • appending tables with the collect command

    Dear Stata Forum,

    I am just starting to experiment with the new "collect" command, and I am having trouble appending results. Does anyone know how to modify the code below so that the results from both table commands are included in the excel file? I know that the appended results will not be pretty (the two tables don't even have the same number of columns). I just want to get them output as one chunk.

    Thanks,

    Jeremy

    Code:
    sysuse nlsw88.dta
    
    collect clear
    table race, stat(freq) stat(percent) 
    table race married, stat(mean age) append 
    collect export table1.xlsx, replace

  • #2
    See:
    Code:
    help collect get

    Comment


    • #3
      Hi Alan,

      Thanks for the suggestion. I haven't read all 260+ pages of the help file yet. However, I have read the parts that seemed most relevant. The table command automatically saves results in a collection (just like the "collect get" command) with the default name "Table." As I understand it, the append option should add the current results to the results already saved. This made me expect that the code I wrote would produce an excel file containing the results from both table commands, but the excel file only contains the second set of results. I've tried a number of different approaches, but I haven't found one that works. Maybe there is another section of the help file I should be reading . . .

      Jeremy

      Comment


      • #4
        This code shows more explicitly that the results are being collected, but despite the append option, the excel file still only has the results from the second command.
        Jeremy

        Code:
        sysuse nlsw88.dta
        
        collect clear
        collect: table race, stat(freq) stat(percent) 
        collect: table race married, stat(mean age) append 
        collect export table1.xlsx, replace

        Comment


        • #5
          Originally posted by Jeremy Reynolds View Post
          This code shows more explicitly that the results are being collected, but despite the append option, the excel file still only has the results from the second command.
          Jeremy

          Code:
          sysuse nlsw88.dta
          
          collect clear
          collect: table race, stat(freq) stat(percent)
          collect: table race married, stat(mean age) append
          collect export table1.xlsx, replace
          Jeremy Reynolds did you ever find a solution to this? I am strugling with the same issue as you did in this post. In my understanding, the append option only works on adding extra statistics to the same variable.

          Comment


          • #6
            I'm not sure what Jeremy would have wanted to create, but this code creates a table that has the results from both commands:

            Code:
            clear
            sysuse nlsw88.dta
            
            collect clear
            collect: table race, stat(freq) stat(percent)
            collect: table race married, stat(mean age) append
            
            collect layout (race) (married result[frequency percent])
            which produces:
            Code:
            . collect preview
            
            ---------------------------------------------------------------
                    |              Married              Frequency   Percent
                    |    Single    Married      Total                      
            --------+------------------------------------------------------
            Race    |                                                      
              White |  39.41478   39.21217   39.27245       1,637     72.89
              Black |   38.9288   38.67883   38.81132         583     25.96
              Other |      38.5   39.66667   39.30769          26      1.16
              Total |  39.21891    39.1165   39.15316       2,246    100.00
            ---------------------------------------------------------------

            Comment


            • #7
              Thank you!

              The added collect layout command did the trick.
              Here is the code with an extra line to export the table to excel and a line to open the excel workbook (other users may have to edit the last line to point at the excel.exe file).

              Jeremy

              Code:
              sysuse nlsw88.dta
              collect clear
              table race, stat(freq) stat(percent) 
              table race married, stat(mean age) append 
              collect layout (race) (married result[frequency percent])
              
              collect export table1.xlsx, replace
              winexec "C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE" table1.xlsx

              Comment


              • #8
                Hi there, I am trying to create the same table but append the second at the bottom. I am trying to create two tables and append them as Panel A and B. My current command is as follows but this only shows the second option. How can I export a table of percentages to an excel/latex file using the collect option? My code is similar to the following.

                collect clear

                table race gender, stat(percent)

                table race gender, statistic(percent, across(gender)) append

                collect export table1.xlsx, replace

                The desired outcome would look something like the below table:
                Click image for larger version

Name:	Screenshot 2023-03-31 at 11.57.41 AM.png
Views:	2
Size:	79.8 KB
ID:	1707999

                Comment

                Working...
                X