Announcement

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

  • Create a stata dataset from tab command

    Hi all,
    Is it possible to transform the output generated from STATA's tab command to the ".dta" dataset?
    It would be great if anyone could help transforming the following STATA output to .dta dataset.
    Code:
    sysuse auto, clear
    tab make foreign, nof col
    The dataset should ideally comprise 4 columns: make, column percentages of domestic and foreign, and total percentage.

  • #2
    One way is to use collapse after creating the relevant variables.

    Code:
    sysuse auto, clear
    tab make foreign, nof col
    
    egen count1= total(!foreign)
    egen count2= total(foreign)
    gen Domestic=(!foreign*100)/count1
    gen Foreign=(foreign*100)/count2
    gen Total=100/_N
    collapse (sum) Domestic Foreign Total, by(make)
    format Domestic-Total %3.2f
    Res.:

    Code:
    . tab make foreign, nof col
    
                       |      Car origin
        Make and model |  Domestic    Foreign |     Total
    -------------------+----------------------+----------
           AMC Concord |      1.92       0.00 |      1.35 
             AMC Pacer |      1.92       0.00 |      1.35 
            AMC Spirit |      1.92       0.00 |      1.35 
             Audi 5000 |      0.00       4.55 |      1.35 
              Audi Fox |      0.00       4.55 |      1.35 
              BMW 320i |      0.00       4.55 |      1.35 
         Buick Century |      1.92       0.00 |      1.35 
         Buick Electra |      1.92       0.00 |      1.35 
         Buick LeSabre |      1.92       0.00 |      1.35 
            Buick Opel |      1.92       0.00 |      1.35 
           Buick Regal |      1.92       0.00 |      1.35 
         Buick Riviera |      1.92       0.00 |      1.35 
         Buick Skylark |      1.92       0.00 |      1.35 
          Cad. Deville |      1.92       0.00 |      1.35 
         Cad. Eldorado |      1.92       0.00 |      1.35 
          Cad. Seville |      1.92       0.00 |      1.35 
        Chev. Chevette |      1.92       0.00 |      1.35 
          Chev. Impala |      1.92       0.00 |      1.35 
          Chev. Malibu |      1.92       0.00 |      1.35 
     Chev. Monte Carlo |      1.92       0.00 |      1.35 
           Chev. Monza |      1.92       0.00 |      1.35 
            Chev. Nova |      1.92       0.00 |      1.35 
            Datsun 200 |      0.00       4.55 |      1.35 
            Datsun 210 |      0.00       4.55 |      1.35 
            Datsun 510 |      0.00       4.55 |      1.35 
            Datsun 810 |      0.00       4.55 |      1.35 
            Dodge Colt |      1.92       0.00 |      1.35 
        Dodge Diplomat |      1.92       0.00 |      1.35 
          Dodge Magnum |      1.92       0.00 |      1.35 
       Dodge St. Regis |      1.92       0.00 |      1.35 
           Fiat Strada |      0.00       4.55 |      1.35 
           Ford Fiesta |      1.92       0.00 |      1.35 
          Ford Mustang |      1.92       0.00 |      1.35 
          Honda Accord |      0.00       4.55 |      1.35 
           Honda Civic |      0.00       4.55 |      1.35 
     Linc. Continental |      1.92       0.00 |      1.35 
          Linc. Mark V |      1.92       0.00 |      1.35 
      Linc. Versailles |      1.92       0.00 |      1.35 
             Mazda GLC |      0.00       4.55 |      1.35 
          Merc. Bobcat |      1.92       0.00 |      1.35 
          Merc. Cougar |      1.92       0.00 |      1.35 
         Merc. Marquis |      1.92       0.00 |      1.35 
         Merc. Monarch |      1.92       0.00 |      1.35 
            Merc. XR-7 |      1.92       0.00 |      1.35 
          Merc. Zephyr |      1.92       0.00 |      1.35 
               Olds 98 |      1.92       0.00 |      1.35 
        Olds Cutl Supr |      1.92       0.00 |      1.35 
          Olds Cutlass |      1.92       0.00 |      1.35 
         Olds Delta 88 |      1.92       0.00 |      1.35 
            Olds Omega |      1.92       0.00 |      1.35 
         Olds Starfire |      1.92       0.00 |      1.35 
         Olds Toronado |      1.92       0.00 |      1.35 
           Peugeot 604 |      0.00       4.55 |      1.35 
           Plym. Arrow |      1.92       0.00 |      1.35 
           Plym. Champ |      1.92       0.00 |      1.35 
         Plym. Horizon |      1.92       0.00 |      1.35 
         Plym. Sapporo |      1.92       0.00 |      1.35 
          Plym. Volare |      1.92       0.00 |      1.35 
        Pont. Catalina |      1.92       0.00 |      1.35 
        Pont. Firebird |      1.92       0.00 |      1.35 
      Pont. Grand Prix |      1.92       0.00 |      1.35 
         Pont. Le Mans |      1.92       0.00 |      1.35 
         Pont. Phoenix |      1.92       0.00 |      1.35 
         Pont. Sunbird |      1.92       0.00 |      1.35 
        Renault Le Car |      0.00       4.55 |      1.35 
                Subaru |      0.00       4.55 |      1.35 
         Toyota Celica |      0.00       4.55 |      1.35 
        Toyota Corolla |      0.00       4.55 |      1.35 
         Toyota Corona |      0.00       4.55 |      1.35 
             VW Dasher |      0.00       4.55 |      1.35 
             VW Diesel |      0.00       4.55 |      1.35 
             VW Rabbit |      0.00       4.55 |      1.35 
           VW Scirocco |      0.00       4.55 |      1.35 
             Volvo 260 |      0.00       4.55 |      1.35 
    -------------------+----------------------+----------
                 Total |    100.00     100.00 |    100.00 
    
    . 
     
    . format Domestic-Total %3.2f
    
    . l, sep(0)
    
         +------------------------------------------------+
         | make                Domestic   Foreign   Total |
         |------------------------------------------------|
      1. | AMC Concord             1.92      0.00    1.35 |
      2. | AMC Pacer               1.92      0.00    1.35 |
      3. | AMC Spirit              1.92      0.00    1.35 |
      4. | Audi 5000               0.00      4.55    1.35 |
      5. | Audi Fox                0.00      4.55    1.35 |
      6. | BMW 320i                0.00      4.55    1.35 |
      7. | Buick Century           1.92      0.00    1.35 |
      8. | Buick Electra           1.92      0.00    1.35 |
      9. | Buick LeSabre           1.92      0.00    1.35 |
     10. | Buick Opel              1.92      0.00    1.35 |
     11. | Buick Regal             1.92      0.00    1.35 |
     12. | Buick Riviera           1.92      0.00    1.35 |
     13. | Buick Skylark           1.92      0.00    1.35 |
     14. | Cad. Deville            1.92      0.00    1.35 |
     15. | Cad. Eldorado           1.92      0.00    1.35 |
     16. | Cad. Seville            1.92      0.00    1.35 |
     17. | Chev. Chevette          1.92      0.00    1.35 |
     18. | Chev. Impala            1.92      0.00    1.35 |
     19. | Chev. Malibu            1.92      0.00    1.35 |
     20. | Chev. Monte Carlo       1.92      0.00    1.35 |
     21. | Chev. Monza             1.92      0.00    1.35 |
     22. | Chev. Nova              1.92      0.00    1.35 |
     23. | Datsun 200              0.00      4.55    1.35 |
     24. | Datsun 210              0.00      4.55    1.35 |
     25. | Datsun 510              0.00      4.55    1.35 |
     26. | Datsun 810              0.00      4.55    1.35 |
     27. | Dodge Colt              1.92      0.00    1.35 |
     28. | Dodge Diplomat          1.92      0.00    1.35 |
     29. | Dodge Magnum            1.92      0.00    1.35 |
     30. | Dodge St. Regis         1.92      0.00    1.35 |
     31. | Fiat Strada             0.00      4.55    1.35 |
     32. | Ford Fiesta             1.92      0.00    1.35 |
     33. | Ford Mustang            1.92      0.00    1.35 |
     34. | Honda Accord            0.00      4.55    1.35 |
     35. | Honda Civic             0.00      4.55    1.35 |
     36. | Linc. Continental       1.92      0.00    1.35 |
     37. | Linc. Mark V            1.92      0.00    1.35 |
     38. | Linc. Versailles        1.92      0.00    1.35 |
     39. | Mazda GLC               0.00      4.55    1.35 |
     40. | Merc. Bobcat            1.92      0.00    1.35 |
     41. | Merc. Cougar            1.92      0.00    1.35 |
     42. | Merc. Marquis           1.92      0.00    1.35 |
     43. | Merc. Monarch           1.92      0.00    1.35 |
     44. | Merc. XR-7              1.92      0.00    1.35 |
     45. | Merc. Zephyr            1.92      0.00    1.35 |
     46. | Olds 98                 1.92      0.00    1.35 |
     47. | Olds Cutl Supr          1.92      0.00    1.35 |
     48. | Olds Cutlass            1.92      0.00    1.35 |
     49. | Olds Delta 88           1.92      0.00    1.35 |
     50. | Olds Omega              1.92      0.00    1.35 |
     51. | Olds Starfire           1.92      0.00    1.35 |
     52. | Olds Toronado           1.92      0.00    1.35 |
     53. | Peugeot 604             0.00      4.55    1.35 |
     54. | Plym. Arrow             1.92      0.00    1.35 |
     55. | Plym. Champ             1.92      0.00    1.35 |
     56. | Plym. Horizon           1.92      0.00    1.35 |
     57. | Plym. Sapporo           1.92      0.00    1.35 |
     58. | Plym. Volare            1.92      0.00    1.35 |
     59. | Pont. Catalina          1.92      0.00    1.35 |
     60. | Pont. Firebird          1.92      0.00    1.35 |
     61. | Pont. Grand Prix        1.92      0.00    1.35 |
     62. | Pont. Le Mans           1.92      0.00    1.35 |
     63. | Pont. Phoenix           1.92      0.00    1.35 |
     64. | Pont. Sunbird           1.92      0.00    1.35 |
     65. | Renault Le Car          0.00      4.55    1.35 |
     66. | Subaru                  0.00      4.55    1.35 |
     67. | Toyota Celica           0.00      4.55    1.35 |
     68. | Toyota Corolla          0.00      4.55    1.35 |
     69. | Toyota Corona           0.00      4.55    1.35 |
     70. | VW Dasher               0.00      4.55    1.35 |
     71. | VW Diesel               0.00      4.55    1.35 |
     72. | VW Rabbit               0.00      4.55    1.35 |
     73. | VW Scirocco             0.00      4.55    1.35 |
     74. | Volvo 260               0.00      4.55    1.35 |
         +------------------------------------------------+

    Comment


    • #3
      Code:
      clear all
      sysuse auto
      tab make foreign, nof col
      
      frame copy default tab
      frame change tab
      contract make foreign , freq(percent) zero nomiss
      reshape wide percent, i(make) j(foreign)
      gen percent = percent0 + percent1
      
      sum percent0 , meanonly
      replace percent0 = percent0/r(sum)*100
      sum percent1, meanonly
      replace percent1 = percent1/r(sum)*100
      sum percent, meanonly
      replace percent  = percent /r(sum)*100
      ---------------------------------
      Maarten L. Buis
      University of Konstanz
      Department of history and sociology
      box 40
      78457 Konstanz
      Germany
      http://www.maartenbuis.nl
      ---------------------------------

      Comment


      • #4
        See also groups from the Stata Journal.

        Comment


        • #5
          and, one way transforming the output to dta
          Code:
          sysuse auto, clear
          
          tempfile tmp 
          
          qui {
              tempfile tmp
              log using "`tmp'" , text 
              noi tab make foreign, nof col
              log close
          }
          
          import delim using "`tmp'" , delim("|") clear 
          drop if strpos(v1,"-+-")
          
          * some clean up
             
          local v2lab = strtoname(trim(itrim(v2[1])))
          drop in 1  
          split v2 
          drop v2 
          order *, alphabetic
          
          foreach v of varlist * {
              
              rename `v' `=strtoname(trim(`v'[1]))'   
          }
          
          rename Domestic Domestic_`v2lab'
          rename Foreign Foreign_`v2lab'
          
          drop in 1
          compress 
          destring Domestic* Foreign* Total, replace 
          
          l in 1/5 , clean abbr(32)
          Code:
                      Make_and_model   Domestic_Car_origin   Foreign_Car_origin   Total  
            1.          AMC Concord                   1.92                    0    1.35  
            2.            AMC Pacer                   1.92                    0    1.35  
            3.           AMC Spirit                   1.92                    0    1.35  
            4.            Audi 5000                      0                 4.55    1.35  
            5.             Audi Fox                      0                 4.55    1.35

          Comment


          • #6
            Thank you all for your approaches. These all are helpful.

            Comment

            Working...
            X