Announcement

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

  • Repeating 'tab' command for several variables

    Happy New Year to everyone!

    I am trying to figure how to repeat the tab command for several IVs (Age, area, education, wealth) using the same DV (Child.dead). This could save a lot of time when there are many IVs. I guess the solution is to use the "foreach" which I'm struggling to put in use.

    Thanks in advance for your insight!

    [CODE]
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double(Child_dead Age Urban_rural Eduction Wealth_quintile)
    0 3 1 1 1
    1 3 1 1 3
    0 5 1 1 1
    0 2 1 3 1
    0 1 1 3 1
    0 2 1 4 2
    0 2 1 1 1
    1 3 1 1 2
    0 2 1 1 1
    0 5 1 1 4
    0 1 1 3 4
    0 2 1 4 4
    0 3 1 1 4
    0 6 1 1 1
    0 1 1 3 1
    0 7 1 2 2
    0 1 1 3 2
    0 1 1 2 2
    0 4 1 1 1
    0 1 1 1 1
    0 3 1 1 2
    0 6 1 1 1
    0 1 1 2 1
    0 1 1 1 1
    1 3 1 1 2
    0 2 1 3 2
    0 6 1 1 3
    0 1 1 3 3
    1 4 1 1 2
    1 6 1 1 2
    0 4 1 2 4
    0 4 1 1 4
    0 2 1 3 5
    0 3 1 4 5
    1 4 1 2 5
    0 2 1 2 5
    0 4 1 4 5
    0 3 1 4 5
    0 6 1 2 5
    0 1 1 1 5
    0 1 1 4 5
    0 2 1 2 4
    0 3 1 3 4
    0 5 1 2 4
    0 2 1 3 4
    0 5 1 3 4
    0 5 1 2 5
    0 4 1 3 5
    0 1 1 3 5
    0 2 1 4 5
    1 2 1 1 5
    0 3 1 2 5
    0 6 1 1 5
    0 1 1 3 5
    0 2 1 1 5
    0 4 1 1 5
    0 1 1 2 4
    0 5 1 1 4
    0 3 1 1 5
    0 3 1 1 5
    1 3 1 1 5
    1 4 1 1 4
    0 7 1 1 4
    0 2 1 2 4
    0 4 1 1 5
    0 6 1 1 5
    0 1 1 3 5
    0 1 1 3 5
    1 2 1 3 5
    0 3 1 3 3
    0 2 1 1 5
    0 2 1 3 5
    0 4 1 1 4
    0 4 1 1 4
    0 1 1 2 5
    0 2 1 3 5
    0 7 1 2 5
    0 1 1 2 5
    0 1 1 3 5
    0 2 1 3 5
    0 3 1 1 5
    0 3 1 1 5
    0 6 1 1 4
    0 4 1 1 4
    0 3 1 3 5
    0 1 1 3 4
    0 2 1 3 4
    0 1 1 4 4
    0 7 1 1 4
    0 1 1 3 4
    1 7 1 1 4
    1 2 1 3 4
    0 2 1 3 4
    0 7 1 1 3
    0 1 1 1 3
    0 1 1 1 3
    0 3 1 1 3
    0 2 1 1 3
    0 2 1 3 2
    0 6 1 1 1
    end

  • #2
    Code:
    . foreach var of varlist Age Urban_rural Eduction Wealth_quintile {
      2. tab `var' Child_dead
      3. }

    Comment


    • #3
      Originally posted by Joro Kolev View Post
      Code:
      . foreach var of varlist Age Urban_rural Eduction Wealth_quintile {
      2. tab `var' Child_dead
      3. }
      Thanks so much Joro for the quick solution!

      Comment


      • #4
        No loop required.

        Code:
        tab2  Child_dead Age Urban_rural Eduction Wealth_quintile, firstonly
        PS a good idea to

        Code:
        rename Eduction Education

        Comment


        • #5
          Originally posted by Nick Cox View Post
          No loop required.

          Code:
          tab2 Child_dead Age Urban_rural Eduction Wealth_quintile, firstonly
          PS a good idea to

          Code:
          rename Eduction Education
          Wonderful as always. Thanks a lot professor. Is it possible to apply eststo or asdoc options with tab2?

          Comment


          • #6
            The new beta version of asdoc creates really elegant tables. You can download it from my website.
            Code:
            net install asdoc, from(http://fintechprofessor.com) replace
            And adding just asdoc to the beginning of the command line does the magic.

            Code:
            asdoc tab2  Child_dead Age Urban_rural Eduction Wealth_quintile, replace firstonly
            Click image for larger version

Name:	asdoc tab2.png
Views:	1
Size:	26.8 KB
ID:	1476922

            Regards
            --------------------------------------------------
            Attaullah Shah, PhD.
            Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
            FinTechProfessor.com
            https://asdocx.com
            Check out my asdoc program, which sends outputs to MS Word.
            For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

            Comment


            • #7
              Originally posted by Attaullah Shah View Post
              The new beta version of asdoc creates really elegant tables. You can download it from my website.
              Code:
              net install asdoc, from(http://fintechprofessor.com) replace
              And adding just asdoc to the beginning of the command line does the magic.

              Code:
              asdoc tab2 Child_dead Age Urban_rural Eduction Wealth_quintile, replace firstonly
              [ATTACH=CONFIG]n1476922[/ATTACH]
              Thats's wonderful. Thanks for the fine program Attaullah Shah!

              Comment


              • #8
                #5 tab2 is an official command. It doesn't provide hooks for community-contributed commands. With asdoc (SSC, as you are asked to explain) it's the other way round, as Attaullah explained. I can't comment on eststo (SJ). It's a wonderful command I never use.

                Comment


                • #9
                  Originally posted by Attaullah Shah View Post
                  The new beta version of asdoc creates really elegant tables. You can download it from my website.
                  Code:
                  net install asdoc, from(http://fintechprofessor.com) replace
                  And adding just asdoc to the beginning of the command line does the magic.

                  Code:
                  asdoc tab2 Child_dead Age Urban_rural Eduction Wealth_quintile, replace firstonly
                  [ATTACH=CONFIG]n1476922[/ATTACH]
                  I am running tab2 with 6 IVs against pd, and the command is showing the following error:

                  Code:
                  . asdoc tab2 pd ag- emp, firstonly replace
                  
                  -> tabulation of pd by ag  
                  
                  
                   RECODE of |
                  pod (Place |
                          of |  RECODE of age (Current age of respondent)
                   delivery) |         1          2          3          4 |     Total
                  -----------+--------------------------------------------+----------
                        Home |       496      1,642      1,317      1,101 |     4,556 
                    Facility |       422      1,482      1,419      1,129 |     4,452 
                  -----------+--------------------------------------------+----------
                       Total |       918      3,124      2,736      2,230 |     9,008 
                  
                       asdoc_tab_twovars():  3001  expected 14 arguments but received 13
                                   <istmt>:     -  function returned error
                  r(3001);
                  The error persists even if I reduce the number of IVs. Please let me know if there is a workaround. Thank you.

                  Comment


                  • #10
                    The error has been corrected. Please check it again.
                    Code:
                     
                     net install asdoc, from(http://fintechprofessor.com) replace
                    Regards
                    --------------------------------------------------
                    Attaullah Shah, PhD.
                    Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
                    FinTechProfessor.com
                    https://asdocx.com
                    Check out my asdoc program, which sends outputs to MS Word.
                    For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

                    Comment


                    • #11
                      Originally posted by Attaullah Shah View Post
                      The error has been corrected. Please check it again.
                      Code:
                      net install asdoc, from(http://fintechprofessor.com) replace
                      Thanks for your quick response. The error is fixed, however, following the command the tables in output window keep flowing without pause. This seems to be a strange issue as it happens only when I am using the tab2 command. I tried quitely to avoid the noise, but then it keeps the system busy and at the same time the Myfile.doc keeps increasing in size which means that the tables are actually depositing in the document and could go on forever...
                      Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	31.4 KB
ID:	1477404

                      Last edited by Sonnen Blume; 05 Jan 2019, 22:17.

                      Comment


                      • #12
                        Corrected, can you please check it now.
                        Code:
                         
                         net install asdoc, from(http://fintechprofessor.com) replace
                        Regards
                        --------------------------------------------------
                        Attaullah Shah, PhD.
                        Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
                        FinTechProfessor.com
                        https://asdocx.com
                        Check out my asdoc program, which sends outputs to MS Word.
                        For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

                        Comment


                        • #13
                          Originally posted by Attaullah Shah View Post
                          Corrected, can you please check it now.
                          Code:
                          net install asdoc, from(http://fintechprofessor.com) replace
                          Thanks... This error is no longer there, but now tab2 is reporting frequencies and not percentages.

                          The command is:
                          Code:
                           asdoc tab2 pd ag rel anc , col nof replace firstonly
                          This is asdoc output:
                          RECODE of pod (Place of delivery) RECODE of age (Current age of respondent)
                          1 2 3 4 Total
                          Home 496 1642 1317 1101 4556
                          Facility 422 1482 1419 1129 4452
                          Total 918 3124 2736 2230 9008
                          And this Stata output:
                          RECODE of |
                          pod (Place |
                          of | RECODE of age (Current age of respondent)
                          delivery) | 1 2 3 4 | Total
                          -----------+--------------------------------------------+----------
                          Home | 54.03 52.56 48.14 49.37 | 50.58
                          Facility | 45.97 47.44 51.86 50.63 | 49.42
                          -----------+--------------------------------------------+----------
                          Total | 100.00 100.00 100.00 100.00 | 100.00

                          Comment


                          • #14
                            Thanks for pointing that out and helping me in developing and testing the new tab command for asdoc. I have uploaded the revised version. Please test it again.
                            The few tests that I have conducted produce expected results

                            Code:
                            net install asdoc, from(http://fintechprofessor.com) replace
                            sysuse nlsw88
                            asdoc tab2 age race, replace col nof
                            Click image for larger version

Name:	asdoc tab22.png
Views:	1
Size:	51.5 KB
ID:	1477534

                            Regards
                            --------------------------------------------------
                            Attaullah Shah, PhD.
                            Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
                            FinTechProfessor.com
                            https://asdocx.com
                            Check out my asdoc program, which sends outputs to MS Word.
                            For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

                            Comment


                            • #15
                              Originally posted by Attaullah Shah View Post
                              Thanks for pointing that out and helping me in developing and testing the new tab command for asdoc. I have uploaded the revised version. Please test it again.
                              The few tests that I have conducted produce expected results

                              Code:
                              net install asdoc, from(http://fintechprofessor.com) replace
                              sysuse nlsw88
                              asdoc tab2 age race, replace col nof
                              [ATTACH=CONFIG]n1477534[/ATTACH]
                              This is perfect! Thanks for your patient replies...

                              Comment

                              Working...
                              X