Announcement

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

  • Format stata summary output table with multiple variables


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str6 Gender float(age_grp no_currently_on_ART arv_dispensing)
    "M"  5 1 1
    "M"  6 1 1
    "M"  6 1 1
    "F"  4 1 1
    "F"  5 1 1
    "F"  4 1 1
    "F"  5 1 1
    "M"  5 1 1
    "M"  4 1 1
    "M"  4 1 1
    "M"  5 1 1
    "M"  3 1 1
    "F"  4 1 1
    "M"  4 1 1
    "F"  4 1 1
    "F"  5 1 1
    "M"  3 1 1
    "M"  4 1 1
    "M"  3 1 1
    "F"  3 1 1
    "F"  3 1 1
    "M"  2 1 1
    "F"  3 1 1
    "M"  4 1 1
    "F"  3 1 1
    "M"  4 1 1
    "F"  7 1 1
    "M"  9 1 2
    "F"  7 1 1
    "F"  8 1 2
    "M"  8 1 2
    "F"  9 1 1
    "M"  9 1 1
    "M"  9 1 2
    "M" 10 1 1
    "M"  9 1 3
    "M"  7 1 1
    "F" 10 1 3
    "F" 11 1 1
    "M"  9 1 2
    end
    label values age_grp age_grp
    label def age_grp 2 "1-4yrs", modify
    label def age_grp 3 "5-9yrs", modify
    label def age_grp 4 "10-14yrs", modify
    label def age_grp 5 "15-19yrs", modify
    label def age_grp 6 "20-24yrs", modify
    label def age_grp 7 "25-29yrs", modify
    label def age_grp 8 "30-34yrs", modify
    label def age_grp 9 "35-39yrs", modify
    label def age_grp 10 "40-44yrs", modify
    label def age_grp 11 "45-49yrs", modify
    label values arv_dispensing arv_dispensing
    label def arv_dispensing 1 "< 3 months", modify
    label def arv_dispensing 2 " 3-5 months", modify
    label def arv_dispensing 3 " > 6 months", modify
    label var Gender "Gender"
    Using the data above I would like to create a table output in this format, would be nice to export the file to xls format.
    INDICATORS <1 Yr <1 Yr 1-4 Yrs 1-4 Yrs 5-9 Yrs 5-9 Yrs 10-14 Yrs 10-14 Yrs
    Female Male Female Male Female Male Female Male
    Number Currently on ART 0 0 0 0 0 0 0 0
    ARV Dispensing <3 months 0 0 0 0 0 0 0 0
    ARV Dispensing 3-5 Months 0 0 0 0 0 0 0 0
    ARV Dispensing 6 Months or more 0 0 0 0 0 0 0 0

  • #2
    Install estout from Stata Journal / SSC, authored by Ben Jann.

    Code:
    egen group= group(Gender age_grp), label
    estpost tabulate arv_dispensing group
    esttab . using myfile.csv, cell(b) unstack noobs collabels("") nomtitle nonumber replace
    Res.:

    Code:
    
    . esttab ., cell(b) unstack noobs collabels("")  nomtitle nonumber compress
    
    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                F 5-9yrs F 10-14~s F 15-19~s F 25-29~s F 30-34~s F 35-39~s F 40-44~s F 45-49~s  M 1-4yrs  M 5-9yrs M 10-14~s M 15-19~s M 20-24~s M 25-29~s M 30-34~s M 35-39~s M 40-44~s     Total
                                                                                                                                                                                                  
    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    < 3 months         4         4         3         2         0         1         0         1         1         3         6         3         2         1         0         1         1        33
    3-5 months         0         0         0         0         1         0         0         0         0         0         0         0         0         0         1         3         0         5
    > 6 months         0         0         0         0         0         0         1         0         0         0         0         0         0         0         0         1         0         2
    Total              4         4         3         2         1         1         1         1         1         3         6         3         2         1         1         5         1        40
    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    Comment


    • #3
      Thank You Andrew Musau,
      It has worked, but their is a limit to the number of variables I can use in the table, when I add other variable it throws an error too many variables specified. It seems it uses the tabulate so means I can't use more than two variables in the tabulation (is that right?).
      How can I output them F M interchangebly i.e. &lt;1

      Comment


      • #4
        It seems it uses the tabulate so means I can't use more than two variables in the tabulation (is that right?).
        Correct.

        How can I output them F M interchangebly
        Change the order of the grouping variables

        Code:
        egen group= group(age_grp Gender), label

        Comment


        • #5
          Another spin has been thrown to my data presentation. We have numbers that identify facilities called mfl_code, so I will need to format the output further to include the mfl_code

          ----------------------- copy starting from the next line -----------------------
          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input int mfl_code str6 Gender float arv_dispensing
          14133 "M" 1
          14133 "M" 1
          14133 "M" 1
          14133 "F" 1
          14133 "F" 1
          14133 "F" 1
          14133 "F" 1
          14133 "M" 1
          14133 "M" 1
          14133 "M" 1
          14133 "M" 1
          14133 "M" 1
          14133 "F" 1
          14133 "M" 1
          14133 "F" 1
          14133 "F" 1
          14133 "M" 1
          14133 "M" 1
          14133 "M" 1
          14133 "F" 1
          14133 "F" 1
          14133 "M" 1
          14133 "F" 1
          14133 "M" 1
          14133 "F" 1
          14133 "M" 1
          14133 "F" 1
          14133 "M" 2
          14133 "F" 1
          14133 "F" 2
          14133 "M" 2
          14133 "F" 1
          14133 "M" 1
          14133 "M" 2
          14133 "M" 1
          14133 "M" 2
          14133 "M" 1
          14133 "F" 2
          14133 "F" 1
          14133 "M" 2
          14133 "F" 1
          14133 "F" 1
          14133 "F" 1
          14133 "F" 1
          14133 "F" 1
          14133 "F" 1
          14133 "F" 2
          14133 "F" 1
          14133 "F" 1
          14133 "F" 1
          14133 "F" 1
          14133 "F" 1
          14133 "F" 1
          14133 "F" 2
          14133 "F" 1
          14133 "F" 1
          14133 "F" 1
          14133 "F" 1
          14133 "F" 2
          14133 "F" 1
          14133 "F" 1
          14133 "F" 1
          14133 "F" 1
          14133 "F" 2
          14133 "F" 1
          14133 "F" 1
          14133 "F" 2
          14133 "F" 1
          14133 "F" 1
          14133 "F" 1
          14133 "F" 1
          14133 "F" 1
          14133 "F" 1
          14133 "F" 1
          14133 "F" 1
          14133 "F" 2
          14133 "F" 1
          14133 "F" 1
          14133 "F" 1
          14133 "F" 1
          14133 "F" 2
          14133 "F" 1
          14133 "M" 2
          14133 "F" 2
          14133 "F" 1
          14133 "F" 2
          14133 "F" 2
          14133 "F" 2
          14133 "M" 1
          14133 "F" 2
          14133 "F" 2
          14133 "M" 2
          14133 "M" 1
          14133 "F" 1
          14133 "M" 1
          14133 "F" 1
          14133 "M" 2
          14133 "F" 1
          14133 "M" 1
          14133 "F" 2
          end
          label values arv_dispensing arv_dispensing
          label def arv_dispensing 1 "&lt; 3 months", modify
          label def arv_dispensing 2 " 3-5 months", modify
          ------------------ copy up to and including the previous line ------------------

          Listed 100 out of 10475 observations

          mfl_code Arv dispensing < 1F Arv dispensing <1 M arv dispensing 1-9F arv_dispensing 1-9M arv_dispensing 10-14F arv_dispensing 10-14M arv_dispensing 15-19F
          14133 1 1 3 4 4
          The mfl_code can run to 200 facilities. The code shared by @Musau works perfectly, my dilemma is how to factor the different facilities using the mfl_code

          Comment


          • #6
            I do not see how this is different from #1, except that you have the paired variable in the column.

            Code:
            egen group= group(arv_dispensing Gender), label
            estpost tabulate group mfl_code
            esttab . using myfile.csv, cell(b) unstack noobs collabels("") nomtitle nonumber replace

            Comment


            • #7

              There is a difference with #1, in #1 we were tabulating simply the ARV_dispensing vs the age groups. The last part I am including mfl_code in the tabulation, my goal is to have a composite table with arv_dispensing, age group and gender. My challenge is that I can't seem to add a third variable to the table.
              Code:
               egen group= group(arv_dispensing Gender *age_grp), label estpost tabulate group mfl_code esttab . using myfile.csv, cell(b) unstack noobs collabels("") nomtitle nonumber replace

              Comment


              • #8
                There is no age group in the data example in #5. You can loop and produce tables for each mfl_code and then append or in one go, group mfl_code and age group and tabulate the two grouped variables. Otherwise, I do not see any other way of adding a fourth variable in a 2 dimensional table, but it could just be me...

                Comment


                • #9
                  Thanks for your Help. I appreciate you making the time.
                  Regards

                  Comment

                  Working...
                  X