Announcement

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

  • Calling a block of code within a DO file

    Hello All,

    I am running a DO file to create a table of summary statistics for a large number of variable and then displaying their t-test result in a table. My objective is to create publication ready tables directly from Stata in word file, and I am using asdoc file to create it.

    I need to use a block of code repeatedly for different type of summary stats. Instead of repeating the block of code, can create a program or a macro to "call" that block within a do file whenever needed?

    I tried creating a program but it didn't return the output as expected.

    Please see the example here:

    sysuse auto,clear
    asdoc, row(Dependent variable:domestic or foreign, Domestic mean/frequency, Domestic SD, Foreign mean/frequency, Foreign SD, t-test) title(Summary statistics) save(myfile) replace
    asdoc, row( Model independent variables, \i, \i, \i, \i, \i) append
    foreach var of varlist price mpg rep78 headroom trunk weight length turn{
    qui sum `var' if foreign==0
    local mf=`r(mean)'/`r(N)'
    asdoc, accum(`mf', `r(sd)')
    qui sum `var' if foreign==1
    local mf=`r(mean)'/`r(N)'
    asdoc, accum(`mf', `r(sd)')
    ttest `var', by(foreign)
    local t=round(abs(`r(t)'),0.001)
    local t=substr("`t'",1,5)
    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 " "
    }
    asdoc, accum(`tstar')
    asdoc, row(`var', $accum)
    }

    This creates my required table.

    For different summary stats, I need to use the following block of code:
    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 " "
    }

    Is there a way to "call" this block?

    I tried creating a program, and then introduce the program in my main do file. However, doing this completely skips the t-star routine and doesn't show any values in the doc file.

    cap program drop pstars
    program define pstars
    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 " "
    }
    end

    sysuse auto,clear
    asdoc, row(Dependent variable:domestic or foreign, Domestic mean/frequency, Domestic SD, Foreign mean/frequency, Foreign SD, t-test) title(Summary statistics) save(myfile) replace
    asdoc, row( Model independent variables, \i, \i, \i, \i, \i) append
    foreach var of varlist price mpg rep78 headroom trunk weight length turn{
    qui sum `var' if foreign==0
    local mf=`r(mean)'/`r(N)'
    asdoc, accum(`mf', `r(sd)')
    qui sum `var' if foreign==1
    local mf=`r(mean)'/`r(N)'
    asdoc, accum(`mf', `r(sd)')
    ttest `var', by(foreign)
    local t=round(abs(`r(t)'),0.001)
    local t=substr("`t'",1,5)
    pstars
    asdoc, accum(`tstar')
    asdoc, row(`var', $accum)
    }

    Appreciate help.

  • #2
    Welcome to Statlist.

    Below is a modified version of your code that I think does what you hope.

    Basically, you had one misunderstanding. Even though your program pstars and your main code are all in the same do-file, the locals are each "local" to their own part of the do-file. The program pstars does not know the local r(p) from the main code and the main code does not know the local stars from pstars. So to get around that, we send r(p) to pstars as an argument, and we define pstars as an rclass program so it can send the local stars back using return.
    Code:
    capture program drop pstars
    program define pstars, rclass
    if `1'<=0.01 {
        local star "***"
    }
    else if `1'<=0.05 {
        local star "**"
    }
    else if `1''<=0.1 {
        local star "*"
    }
    else {
        local star " "
    }
    return local star `star'
    end
    
    sysuse auto, clear
    asdoc, row(Dependent variable:domestic or foreign, Domestic mean/frequency, Domestic SD, Foreign mean/frequency, Foreign SD, t-test) title(Summary statistics) save(myfile) replace
    asdoc, row( Model independent variables, \i, \i, \i, \i, \i) append
    
    foreach var of varlist price mpg rep78 headroom trunk weight length turn {
    
    qui sum `var' if foreign==0
    local mf=`r(mean)'/`r(N)'
    asdoc, accum(`mf', `r(sd)')
    
    qui sum `var' if foreign==1
    local mf=`r(mean)'/`r(N)'
    asdoc, accum(`mf', `r(sd)')
    
    qui ttest `var', by(foreign)
    local t=round(abs(`r(t)'),0.001)
    local t=substr("`t'",1,5)
    pstars `r(p)'
    local star `r(star)'
    asdoc, accum(`t'`star')
    
    asdoc, row(`var', $accum)
    }
    Last edited by William Lisowski; 28 May 2019, 19:30.

    Comment


    • #3
      Wiliam's solution works.

      An alternative is to put that block in a separate .do file, say makestars.do. After that you can type whenever you need that block of code include makestars.do . For this and similar tips see: http://www.maartenbuis.nl/workshops/...l#slide17.smcl

      ---------------------------------
      Maarten L. Buis
      University of Konstanz
      Department of history and sociology
      box 40
      78457 Konstanz
      Germany
      http://www.maartenbuis.nl
      ---------------------------------

      Comment


      • #4
        Thanks William and Maarten. I had realized that issue was with the way locals were being used, and the ability to carry locals from one block of code to another or by using the locals from one do file into another.

        Maarten Buis your solution works perfectly. This will be very useful for future reference as well.

        William Lisowski Thanks for the input on the rclass programs. For my understanding, the local `1' is input to the program pstars. The argument to pstars is `r(p)' which gets in the same place `1'?
        Another thing, when I run the code, there's an error ".6801850882546103' invalid name" (see attached image as well). Any thoughts on that?

        best,
        Asim
        Attached Files

        Comment


        • #5
          There was an unintended macro closing symbol on line 9, this should run
          Code:
          capture program drop pstars
          program define pstars, rclass
          if `1'<=0.01 {
              local star "***"
          }
          else if `1'<=0.05 {
              local star "**"
          }
          else if `1'<=0.1 {
              local star "*"
          }
          else {
              local star " "
          }
          return local star `star'
          end
          
          sysuse auto, clear
          asdoc, row(Dependent variable:domestic or foreign, Domestic mean/frequency, Domestic SD, Foreign mean/frequency, Foreign SD, t-test) title(Summary statistics) save(myfile) replace
          asdoc, row( Model independent variables, \i, \i, \i, \i, \i) append
          
          foreach var of varlist price mpg rep78 headroom trunk weight length turn {
          
          qui sum `var' if foreign==0
          local mf=`r(mean)'/`r(N)'
          asdoc, accum(`mf', `r(sd)')
          
          qui sum `var' if foreign==1
          local mf=`r(mean)'/`r(N)'
          asdoc, accum(`mf', `r(sd)')
          
          qui ttest `var', by(foreign)
          local t=round(abs(`r(t)'),0.001)
          local t=substr("`t'",1,5)
          pstars `r(p)'
          local star `r(star)'
          asdoc, accum(`t'`star')
          
          asdoc, row(`var', $accum)
          }
          Last edited by Attaullah Shah; 29 May 2019, 12:35.
          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


          • #6
            For my understanding, the local `1' is input to the program pstars. The argument to pstars is `r(p)' which gets in the same place `1'?
            For an understanding of how programs pass arguments and results in and out, the best place to start is with a review of Chapter 18 Programming Stata in the Stata User’s Guide PDF included in your Stata installation and accessible from Stata's Help menu.

            My thanks to Attaullah Shah for finding my typographical error, in a branch that had not been tested on my example data.

            Comment


            • #7
              thanks Attaullah Shah William Lisowski

              Comment

              Working...
              X