Announcement

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

  • Add tables with collect

    Hello, I am trying to stack (add) tables one below the other. Essentially, the tables are the same, but they vary depending on the survey wave.
    The particular issue is that I would like to have row percentages rather than column percentages, which is what dtable provides. Therefore, I start from the following code that I found on this forum:


    collect clear

    local vlist sexo montevideo blanco nivel_educ

    table () (ocup) if ocuallwaves == 1 & allwaves == 1 & ola == 1, ///
    stat(frequency) ///
    stat(percent, across(ocup)) name(N)

    collect addtags N[_hide]
    collect layout (N) (ocup#result)

    foreach v of local vlist {
    table (`v') (ocup), ///
    stat(frequency) ///
    stat(percent, across(ocup)) ///
    totals(`v') ///
    name(`v')
    }

    collect combine all = N `vlist'
    collect composite define stats = frequency percent
    collect style cell result[percent], nformat("%6.2f") sformat("(%s%%)")
    collect style header result[stats], title(hide) level(hide)
    collect layout (N `vlist') (ocup#result[stats])


    . collect layout (N `vlist') (ocup#result[stats])

    Collection: all
    Rows: N sexo montevideo blanco nivel_educ
    Columns: ocup#result[stats]
    Table 1: 13 x 3

    ------------------------------------------------------------------------
    | Employed
    | No Yes Total
    -------------------+----------------------------------------------------
    N | 440 (35.98%) 783 (64.02%) 1,223 (100.00%)
    Sex |
    Male | 3,384 (35.30%) 6,203 (64.70%) 9,587 (100.00%)
    Female | 5,877 (52.27%) 5,366 (47.73%) 11,243 (100.00%)
    Montevideo |
    No | 5,559 (45.74%) 6,595 (54.26%) 12,154 (100.00%)
    Yes | 3,706 (42.67%) 4,979 (57.33%) 8,685 (100.00%)
    White |
    Other | 3,157 (43.48%) 4,104 (56.52%) 7,261 (100.00%)
    White | 6,108 (44.98%) 7,470 (55.02%) 13,578 (100.00%)
    Level of education |
    Low | 7,070 (48.53%) 7,499 (51.47%) 14,569 (100.00%)
    High | 2,195 (35.01%) 4,075 (64.99%) 6,270 (100.00%)
    ------------------------------------------------------------------------




    However, this code only applies to the first wave (variable “ola”). I would like to obtain the same information for waves 3 and 4, and stack the resulting tables below the first one.
    At the same time, I would like to cross the same variables in each wave with the variable “formal” (a binary variable), so as to display six columns (including totals).


    . dataex ola sexo montevideo blanco nivel_educ ocup formal ocuallwaves allwaves

    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float ola byte sexo float(montevideo blanco nivel_educ ocup formal ocuallwaves allwaves)
    1 1 1 0 0 . . . .
    1 0 1 0 0 1 0 1 .
    1 1 1 1 0 0 . 1 1
    1 1 1 0 0 . . . .
    1 1 1 0 0 . . . .
    3 1 1 0 0 . . . .
    3 0 1 0 0 1 1 1 .
    3 1 1 1 0 1 1 1 1
    3 1 1 0 0 0 . . .
    3 1 1 0 0 . . . .
    3 0 1 0 0 . . . .
    4 1 1 0 1 0 . . .
    4 0 1 0 0 1 1 1 .
    4 1 1 1 0 1 1 1 1
    4 1 1 0 1 0 . . .
    4 1 1 0 0 . . . .
    4 0 1 0 0 . . . .
    4 1 1 0 1 0 . . .
    1 1 1 1 0 . . . .
    1 0 1 0 0 1 0 . .
    1 1 1 1 0 0 . 1 1
    1 0 1 1 0 . . . .
    3 1 1 1 0 0 . . .
    3 1 1 1 0 1 1 1 1
    3 0 1 1 0 0 . . .
    3 0 1 1 0 1 0 . .
    4 1 1 1 0 0 . . .
    4 1 1 1 0 1 1 1 1
    4 0 1 1 0 1 0 . .
    4 0 1 0 0 1 0 . .
    4 1 1 1 0 . . . .
    4 0 1 1 0 . . . .
    4 0 1 1 0 . . . .
    4 1 1 1 0 . . . .
    4 1 1 1 0 . . . .
    1 0 1 0 0 . . . .
    1 0 1 1 0 1 0 1 .
    1 1 1 0 0 0 . 1 1
    1 1 1 1 0 . . . .
    1 0 1 0 0 . . . .
    1 1 1 1 0 . . . .
    3 0 1 0 0 0 . . .
    3 0 1 1 0 1 0 1 .
    3 1 1 0 0 0 . 1 1
    3 1 1 0 0 1 1 . .
    3 0 1 0 0 0 . . .
    3 1 1 0 0 1 1 . .
    4 0 1 0 0 0 . . .
    4 0 1 1 0 1 1 1 .
    4 1 1 0 0 0 . 1 1
    4 1 1 1 0 0 . . .
    4 0 1 0 0 0 . . .
    4 1 1 1 0 0 . . .
    4 0 1 0 0 1 1 . .
    4 0 1 0 0 . . . .
    1 1 1 1 0 . . . .
    1 0 1 0 0 1 0 . .
    1 1 1 1 0 0 . 1 1
    1 1 1 1 0 . . . .
    3 1 1 1 1 0 . . .
    3 1 1 1 0 0 . 1 1
    3 1 1 1 0 . . . .
    4 1 1 1 0 1 0 . .
    4 1 1 1 0 0 . 1 1
    4 1 1 1 0 0 . . .
    1 1 1 1 0 . . . .
    1 0 1 1 0 1 0 1 .
    1 1 1 0 0 0 . 1 1
    3 1 1 1 0 0 . . .
    3 0 1 0 0 1 0 1 .
    3 1 1 0 0 0 . 1 1
    3 1 1 1 0 . . . .
    4 1 1 1 0 0 . . .
    4 0 1 1 0 1 0 1 .
    4 1 1 0 0 0 . 1 1
    4 1 1 1 0 . . . .
    1 0 1 1 0 . . . .
    1 0 1 1 0 1 0 1 .
    1 1 1 1 0 0 . 1 1
    1 1 1 1 0 . . . .
    1 1 1 0 0 . . . .
    1 0 1 0 0 . . . .
    3 0 1 0 0 0 . . .
    3 0 1 0 0 1 0 1 .
    3 1 1 0 0 1 0 1 1
    3 1 1 0 0 0 . . .
    3 0 1 0 0 . . . .
    4 0 1 1 0 0 . . .
    4 0 1 1 0 1 0 1 .
    4 1 1 1 0 1 0 1 1
    4 1 1 1 0 0 . . .
    4 0 1 1 0 . . . .
    1 1 1 0 0 . . . .
    1 0 1 0 0 1 1 . .
    1 1 1 0 1 0 . . .
    1 0 1 0 0 . . . .
    1 0 1 1 0 . . . .
    1 0 1 1 0 1 0 1 1
    1 1 1 1 0 0 . 1 .
    1 0 1 1 0 . . . .
    end
    label values ola ola
    label def ola 1 "Ola 1", modify
    label def ola 3 "Ola 3", modify
    label def ola 4 "Ola 4", modify
    label values sexo sexo
    label def sexo 0 "Male", modify
    label def sexo 1 "Female", modify
    label values montevideo montevideo
    label def montevideo 1 "Yes", modify
    label values blanco blanco
    label def blanco 0 "Other", modify
    label def blanco 1 "White", modify
    label values nivel_educ nivel_educ
    label def nivel_educ 0 "Low", modify
    label def nivel_educ 1 "High", modify
    label values ocup ocup
    label def ocup 0 "No", modify
    label def ocup 1 "Yes", modify
    label values formal formal
    label def formal 0 "Informal", modify
    label def formal 1 "Formal", modify
    label values ocuallwaves ocuallwaves
    label def ocuallwaves 1 "Yes", modify
    label values allwaves allwaves
    label def allwaves 1 "Yes", modify
    ------------------ copy up to and including the previous line ------------------

    Listed 100 out of 33667 observations
    Use the count() option to list more


    Thanks.

  • #2
    Solved

    Comment


    • #3
      It would be a service to this community if you shared the solution you found. After all, you are almost certainly not the only person who will ever confront this difficulty, and if you post your solution, others who come here similarly situated could benefit from what you have learned.

      Comment


      • #4
        Hi Clyde, you're totally right.
        The solution I used relies on the following layout:

        collect layout (ola[1]#sexo ola[1]#montevideo ola[1]#blanco ola[1]#nivel_educ ola[3]#sexo ola[3]#montevideo ola[3]#blanco ola[3]#nivel_educ ola[4]#sexo ola[4]#montevideo ola[4]#blanco ola[4]#nivel_educ) (ocup#result[stats] formal#result[stats])
        Thanks for the comment!

        Comment


        • #5
          Guest, I am unable to replicate your solution. Executing the command in #4 gives the following:

          Code:
          (dimension ola not found)
          (dimension formal not found)
          
          Collection: all
                Rows: ola[1]#sexo ola[1]#montevideo ola[1]#blanco ola[1]#nivel_educ ola[3]#sexo ola[3]#montevideo
                      ola[3]#blanco ola[3]#nivel_educ ola[4]#sexo ola[4]#montevideo ola[4]#blanco ola[4]#nivel_educ
             Columns: ocup#result[stats] formal#result[stats]
          
          Your layout specification does not uniquely match any items. One or more of the following dimensions might help
          uniquely match items: across, blanco, collection, nivel_educ, sexo, statcmd.
          Last edited by sladmin; 30 Jan 2026, 14:54. Reason: Anonymize original poster

          Comment

          Working...
          X