Announcement

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

  • Combine dtables to obtain a table with panels using collect

    Hi!! I'm struggling against a combination of previously combined tables.
    My objective is obtain 1 table with two panels: Ola 3 and Ola 4, with this 2 vars in rows, and mean, median & ttest in columnis for every control var.
    This is my code that, obviously, it's wrong.

    preserve
    collect clear

    local controles sexo montevideo blanco nivel_educ
    local ola3 ""
    local ola4 ""

    foreach con of local controles {

    dtable menos_impuestos destino if ola == 3 & adulto_joven == 0, ///
    by(`con', notot test) ///
    continuous(, statistics(mean sd)) ///
    nformat(%9.2f mean sd) ///
    sformat((%s) sd) ///
    name(`con'3)
    collect label levels ola 3 "Ola 3", modify

    dtable menos_impuestos destino if ola == 4 & adulto_joven == 0, ///
    by(`con', notot test) ///
    continuous(, statistics(mean sd)) ///
    nformat(%9.2f mean sd) ///
    sformat((%s) sd) ///
    name(`con'4)

    local ola3 `ola3' `con'3
    local ola4 `ola4' `con'4
    }

    collect combine ola3 = `ola3', style(right)
    collect combine ola4 = `ola4', style(right)

    collect dir
    collect dims
    collect style tex, nocentering
    collect layout (var) (sexo#result montevideo#result blanco#result nivel_educ#result)

    restore



    Something like this but with two panels according Ola3 and Ola4 below.

    Click image for larger version

Name:	Captura de pantalla 2025-12-17 171527.png
Views:	1
Size:	55.2 KB
ID:	1783795





    Data example:

    . dataex menos_impuestos destino sexo montevideo blanco nivel_educ ola adulto_joven

    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(menos_impuestos destino) byte sexo float(montevideo blanco nivel_educ ola adulto_joven)
    . . 1 1 0 0 1 1
    . . 0 1 0 0 1 .
    . . 1 1 0 0 1 0
    . . 1 1 0 0 1 .
    . . 1 1 0 0 1 .
    . . 1 1 0 0 3 1
    . . 0 1 0 0 3 .
    0 0 1 1 1 0 3 0
    . . 1 1 0 0 3 .
    . . 1 1 0 0 3 .
    . . 0 1 0 0 3 .
    1 1 1 1 0 1 4 1
    . . 0 1 0 0 4 .
    1 0 1 1 1 0 4 0
    . . 1 1 0 1 4 .
    . . 1 1 0 0 4 .
    . . 0 1 0 0 4 .
    . . 1 1 0 1 4 .
    . . 1 1 0 0 1 1
    . . 0 1 0 0 1 .
    . . 1 1 0 0 1 0
    . . 0 1 0 0 1 .
    . . 1 1 1 0 3 1
    . . 0 1 1 0 3 .
    1 1 1 1 1 0 3 0
    . . 0 1 1 0 3 .
    . . 0 1 1 0 3 .
    0 0 1 1 1 1 4 1
    . . 0 1 1 0 4 .
    1 0 1 1 1 0 4 0
    . . 0 1 1 0 4 .
    . . 0 1 1 0 4 .
    . . 1 0 0 0 1 1
    . . 1 0 0 1 1 .
    . . 1 0 0 1 1 0
    . . 1 0 0 0 1 .
    . . 0 0 0 0 1 .
    . . 1 0 1 0 3 1
    0 0 1 0 1 1 3 0
    . . 1 0 1 1 3 .
    . . 1 0 1 0 3 .
    . . 0 0 1 0 3 .
    0 0 1 0 1 1 4 1
    . . 1 0 1 1 4 .
    0 0 1 0 1 1 4 0
    . . 1 0 1 0 4 .
    . . 1 0 0 0 1 1
    . . 1 0 0 1 1 0
    . . 0 0 0 0 1 .
    . . 1 0 0 0 1 .
    . . 1 0 1 0 3 1
    1 1 1 0 1 1 3 0
    . . 0 0 1 1 3 .
    . . 1 0 1 0 3 .
    1 0 1 0 1 0 4 1
    0 0 1 0 0 0 4 0
    . . 0 0 1 1 4 .
    . . 1 0 1 1 4 .
    . . 1 1 0 0 1 1
    . . 0 1 0 1 1 .
    . . 1 1 0 1 1 0
    . . 1 1 0 0 1 .
    . . 1 1 1 0 3 1
    1 1 1 1 1 1 3 0
    . . 1 1 1 0 3 .
    1 1 1 1 1 1 4 1
    0 0 1 1 1 1 4 0
    . . 1 1 1 0 4 .
    . . 0 1 0 0 1 1
    . . 0 1 0 0 1 .
    . . 1 1 0 1 1 0
    . . 0 1 1 0 3 1
    . . 0 1 1 0 3 .
    . 0 1 1 1 1 3 0
    1 0 0 1 1 1 4 1
    . . 0 1 1 0 4 .
    1 0 1 1 1 1 4 0
    . . 1 1 0 0 1 1
    . . 0 1 0 0 1 .
    . . 0 1 0 0 1 .
    . . 1 1 0 0 1 0
    . . 1 1 1 0 3 1
    . . 0 1 1 0 3 .
    . . 0 1 1 0 3 .
    1 1 1 1 1 1 3 0
    . . 0 1 1 0 3 .
    0 0 1 1 1 0 4 1
    . . 0 1 1 0 4 .
    . . 0 1 1 1 4 .
    0 0 1 1 1 1 4 0
    . . 0 1 1 0 4 .
    . . 0 1 1 0 4 .
    . . 0 1 0 0 1 1
    . . 1 1 0 0 1 .
    . . 1 1 0 0 1 0
    . . 0 1 1 0 3 1
    1 1 1 1 1 0 3 0
    . . 0 1 1 0 4 1
    1 1 1 1 1 0 4 0
    . . 0 1 1 0 4 .
    end
    label values menos_impuestos menos_impuestos
    label def menos_impuestos 0 "Nunca justificado", modify
    label def menos_impuestos 1 "Siempre justificado", modify
    label values destino destino
    label def destino 0 "Destino", modify
    label def destino 1 "Decisión Propia", modify
    label values sexo sexo
    label def sexo 0 "Hombre", modify
    label def sexo 1 "Mujer", modify
    label values montevideo montevideo
    label def montevideo 0 "No", modify
    label def montevideo 1 "Si", modify
    label values blanco blanco
    label def blanco 0 "Otro", modify
    label def blanco 1 "Blanco", modify
    label values nivel_educ nivel_educ
    label def nivel_educ 0 "Baja", modify
    label def nivel_educ 1 "Alta", modify
    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 adulto_joven adulto_joven
    label def adulto_joven 0 "Adulto", modify
    label def adulto_joven 1 "Jóven", modify
    ------------------ copy up to and including the previous line ------------------

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


    Thanks.

  • #2
    I'll assume you want to stack the Ola 3 items above the Ola 4 items.

    Highlighted in blue are my edits to your code.
    Code:
    collect clear
    
    local controles sexo montevideo blanco nivel_educ
    local ola3 ""
    local ola4 ""
    
    foreach con of local controles {
        dtable menos_impuestos destino if ola == 3 & adulto_joven == 0, ///
            by(`con', notot test) ///
            continuous(, statistics(mean sd)) ///
            nformat(%9.2f mean sd) ///
            sformat((%s) sd) ///
            name(`con'3)
        collect addtags ola[3], name(`con'3)
        local ola3 `ola3' `con'3
    
        dtable menos_impuestos destino if ola == 4 & adulto_joven == 0, ///
            by(`con', notot test) ///
            continuous(, statistics(mean sd)) ///
            nformat(%9.2f mean sd) ///
            sformat((%s) sd) ///
            name(`con'4)
        collect addtags ola[4], name(`con'4)
        local ola4 `ola4' `con'4
    }
    
    collect combine all = `ola3' `ola4'
    collect label levels ola 3 "Ola 3" 4 "Ola 4"
    
    collect dir
    collect dims
    collect style tex, nocentering
    collect layout (ola#var) (sexo#result montevideo#result blanco#result nivel_educ#result)
    Here is the resulting table.
    Code:
    --------------------------------------------------------------------------------------------------------------------------
                            sexo                 montevideo                     blanco                     nivel_educ
                         Mujer    Test      No          Si      Test    Blanco    Test    Otro       Baja        Alta     Test
    --------------------------------------------------------------------------------------------------------------------------
    Ola 3
      N                8 (100.0%)        2 (25.0%)   6 (75.0%)        8 (100.0%)                   3 (37.5%)   5 (62.5%)
      menos_impuestos 0.71 (0.49)    . 0.50 (0.71) 0.80 (0.45) 0.513 0.71 (0.49)     .           0.67 (0.58) 0.75 (0.50) 0.846
      destino         0.62 (0.52)    . 0.50 (0.71) 0.67 (0.52) 0.725 0.62 (0.52)     .           0.67 (0.58) 0.60 (0.55) 0.875
    Ola 4
      N                8 (100.0%)        2 (25.0%)   6 (75.0%)         7 (87.5%)       1 (12.5%)   4 (50.0%)   4 (50.0%)
      menos_impuestos 0.50 (0.53)    . 0.00 (0.00) 0.67 (0.52) 0.134 0.57 (0.53) 0.356  0.00 (.) 0.75 (0.50) 0.25 (0.50) 0.207
      destino         0.12 (0.35)    . 0.00 (0.00) 0.17 (0.41) 0.604 0.14 (0.38) 0.736  0.00 (.) 0.25 (0.50) 0.00 (0.00) 0.356
    --------------------------------------------------------------------------------------------------------------------------

    Comment


    • #3
      Jeff, that it's exactly what i was looking for.
      Thanks!

      Comment

      Working...
      X