Announcement

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

  • Custom publication tables using loops

    Hi there,

    I am using a loop to create a customized table. In the left-most column are variable names. There are three columns of variable medians and a final column of p-values for a comparison of medians. My loop is below. When I run it, however, I get a blank table without entries for the four columns of statistics. I think that there may be a problem with the 'accum' function, but I am not sure. Thanks so much in advance! --Kathryn Overton


    Code:

    asdoc, row(Indicator, stat1, stat2, stat3, p-Value) title(Comparison of Indicators by Subpopulation) save(Myfile) replace
    foreach var of varlist var1-var20 {

    local egen m1 = median(`var') if subpopCode==1
    asdoc, accum(`m1')
    local egen m2 = median(`var') if subpopCode==2
    asdoc, accum(`m2')
    local egen m3 = median(`var') if subpopCode==3
    asdoc, accum(`m3')
    median `var', by(subpopCode) medianties(split)
    local prob1 : di %9.3f = abs(`r(p)')

    if `r(p)' <=0.01 {
    local star "***"
    }
    else if `r(p)' <=0.05{
    local star "**"
    }
    else if `r(p)' <=0.1{
    local star "*"
    }
    else {
    local star " "
    }
    local pstar `prob1'`star'
    asdoc, accum(`pstar')
    asdoc, row(`var', $accum)
    }



  • #2
    asdoc is from SSC, as you are asked to explain. It is versatile but I've never used it.

    My guess is that the lines like

    Code:
    local egen m1 = median(`var') if subpopCode==1
    should start with egen not local to get anything done. But that is not sufficient as you need to define local macros like m1 for the next lines to work at all.

    Comment


    • #3
      Welcome to Statalist. Since you have shown interest in using asdoc, I am creating a toy data and a working code for asdoc. However, asdocx is now more powerful and versatile, you may like to explore its features here https://fintechprofessor.com/asdocx/. I shall present a working example for asdocx as well.

      Code:
      * Create a dummy data set to use with the code
      clear
      set obs 100
      
      gen subpopCode = mod(_n,3)+1
      
      forv i = 1 / 20 {
      
          gen var`i' = uniform()
      
      }
      The modified cost is given below for asdoc

      Code:
      asdoc, row(Indicator, stat1, stat2, stat3, p-Value) title(Comparison of Indicators by Subpopulation) save(Myfile) replace
      foreach var of varlist var1-var20 {
      
          qui sum `var' if subpopCode==1, detail
          loc m1 :  di %9.3f = `r(p50)'
      
          asdoc, accum(`m1')
      
          qui sum `var' if subpopCode==2, detail
          loc m2 : di %9.3f = `r(p50)'
          asdoc, accum(`m2')
      
          qui sum `var' if subpopCode==3, detail
          loc m3 :  di %9.3f = `r(p50)'
          asdoc, accum(`m3')
      
      
          median `var', by(subpopCode) medianties(split)
          local prob1 : di %9.3f = abs(`r(p)')
      
          if `r(p)' <=0.01 {
              local star "***"
          }
          else if `r(p)' <=0.05{
              local star "**"
          }
          else if `r(p)' <=0.1{
              local star "*"
          }
          else {
              local star " "
          }
          local pstar `prob1'`star'
          asdoc, accum(`pstar')
          asdoc, row(`var', $accum)
      }
      The generated table looks like this
      Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	65.0 KB
ID:	1659670



      Solution based on asdocx

      Instead of option row, asdocx uses flexmat to create customized tables. The created table can be then exported to excel or word file.
      Code:
      flexmat reset, mode(asdocx)
      flexmat addrow, data(Indicator, stat1, stat2, stat3, p-Value)
      loc nextrow 2
      foreach var of varlist var1-var20 {
      
          qui sum `var' if subpopCode==1, detail
          loc m1 :  di %9.3f = `r(p50)'
      
      
          qui sum `var' if subpopCode==2, detail
          loc m2 : di %9.3f = `r(p50)'
      
          qui sum `var' if subpopCode==3, detail
          loc m3 :  di %9.3f = `r(p50)'
      
      
          median `var', by(subpopCode) medianties(split)
          local prob1 : di %9.3f = abs(`r(p)')
      
          if `r(p)' <=0.01 {
              local star "***"
          }
          else if `r(p)' <=0.05{
              local star "**"
          }
          else if `r(p)' <=0.1{
              local star "*"
          }
          else {
              local star " "
          }
          local pstar `prob1'`star'
          flexmat addrow, data(`var', `m1', `m2', `m3', `pstar') row(`nextrow')
          loc ++nextrow
      }
      
       flexmat addparts, title(Comparison of Indicators by Subpopulation)
      
      * Export to MS Word
       asdocx export
      Click image for larger version

Name:	Word.PNG
Views:	1
Size:	58.1 KB
ID:	1659671


      Code:
      * OR to Excel
      asdocx export, save(MyFile.xlsx)
      Click image for larger version

Name:	Excel.PNG
Views:	1
Size:	47.3 KB
ID:	1659672




      Last edited by Attaullah Shah; 14 Apr 2022, 16:30.
      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


      • #4
        Attaullah,

        This is extremely helpful! Thank you! I was able to run the loop using the 'asdoc' function. One final question, what's the best way to insert the variable labels instead of the raw names in the table?

        Gratefully,
        Kathryn Overton

        Comment


        • #5
          Here is the code for labels

          Code:
          clear
          set obs 100
          
          gen subpopCode = mod(_n,3)+1
          
          forv i = 1 / 20 {
          
              gen var`i' = uniform()
              label var var`i' "Label for var`i'"
          
          }
          asdoc, row(Indicator, stat1, stat2, stat3, p-Value) title(Comparison of Indicators by Subpopulation) save(Myfile) replace
          foreach var of varlist var1-var20 {
          
              qui sum `var' if subpopCode==1, detail
              loc m1 :  di %9.3f = `r(p50)'
          
              asdoc, accum(`m1')
          
              qui sum `var' if subpopCode==2, detail
              loc m2 : di %9.3f = `r(p50)'
              asdoc, accum(`m2')
          
              qui sum `var' if subpopCode==3, detail
              loc m3 :  di %9.3f = `r(p50)'
              asdoc, accum(`m3')
          
          
              median `var', by(subpopCode) medianties(split)
              local prob1 : di %9.3f = abs(`r(p)')
          
              if `r(p)' <=0.01 {
                  local star "***"
              }
              else if `r(p)' <=0.05{
                  local star "**"
              }
              else if `r(p)' <=0.1{
                  local star "*"
              }
              else {
                  local star " "
              }
              local pstar `prob1'`star'
              asdoc, accum(`pstar')
              
              asdoc, row(`: var label `var'', $accum)
          }
          I hope asdoc users will support asdocx development.
          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

          Working...
          X