Announcement

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

  • Make a list table in Stata

    Dear Stata users,

    Thank you in advance. I want to make a table that has all items listed in cells that are grouped by a categorical variable. Let me illustrate with data example and show you what I want to get.

    You can use this data example:
    Code:
    sysuse auto
    replace foreign=3 in 1/15
    label define origin 3 Whatever, modify
    sample 30
    keep make foreign
    tabulate foreign
    Or you can use the following data directly:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str18 make byte foreign
    "VW Diesel"        1
    "Olds Cutlass"     0
    "Cad. Eldorado"    3
    "Audi 5000"        1
    "Olds Cutl Supr"   0
    "Datsun 210"       1
    "Olds Omega"       0
    "Fiat Strada"      1
    "AMC Spirit"       3
    "Toyota Corolla"   1
    "Chev. Malibu"     0
    "Pont. Firebird"   0
    "Mazda GLC"        1
    "Merc. Monarch"    0
    "Honda Accord"     1
    "Pont. Grand Prix" 0
    "Olds 98"          0
    "Volvo 260"        1
    "Buick Opel"       3
    "Toyota Celica"    1
    "Subaru"           1
    "Plym. Champ"      0
    end
    label values foreign origin
    label def origin 0 "Domestic", modify
    label def origin 1 "Foreign", modify
    label def origin 3 "Whatever", modify
    And what I want to get is a table like this:
    Click image for larger version

Name:	20230331114319.png
Views:	1
Size:	12.4 KB
ID:	1707897

  • #2
    https://journals.sagepub.com/doi/pdf...36867X20909698 may help.

    Comment


    • #3
      Thank you very much Nick Cox. Your solution is amazing as always.
      Code:
      clear
      input str18 make byte foreign
      "VW Diesel"        1
      "Olds Cutlass"     0
      "Cad. Eldorado"    3
      "Audi 5000"        1
      "Olds Cutl Supr"   0
      "Datsun 210"       1
      "Olds Omega"       0
      "Fiat Strada"      1
      "AMC Spirit"       3
      "Toyota Corolla"   1
      "Chev. Malibu"     0
      "Pont. Firebird"   0
      "Mazda GLC"        1
      "Merc. Monarch"    0
      "Honda Accord"     1
      "Pont. Grand Prix" 0
      "Olds 98"          0
      "Volvo 260"        1
      "Buick Opel"       3
      "Toyota Celica"    1
      "Subaru"           1
      "Plym. Champ"      0
      end
      label values foreign origin
      label def origin 0 "Domestic", modify
      label def origin 1 "Foreign", modify
      label def origin 3 "Whatever", modify
      
      bysort foreign: gen id=_n
      bysort foreign (id): gen wanted=make[1]
      bysort foreign: replace wanted=wanted[_n-1]+"; "+make if _n>1
      bysort foreign: replace wanted=wanted[_N]
      drop id make
      duplicates drop wanted, force

      Comment

      Working...
      X