Announcement

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

  • Print each command in loop

    Does anyone have suggestions on how to make a more descriptive log file within a loop. For example, if you're running multiple regressions and you want to see the command used to generate the output of each regression, is there a way to log each iteration of the loop? Here's a more detailed example:

    Code:
    log using "example.txt", name(example) text replace
    
    sysuse auto.dta
    
    * Not that helpful in a log file
    forvalues i = 0/1 {
        regress mpg price if foreign == `i'
    }
    
    * More helpful in a log file
    regress mpg price if foreign == 0
    regress mpg price if foreign == 1
    
    log close example
    Here's the output of this script:

    Code:
    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
          name:  example
           log:  example.txt
      log type:  text
     opened on:  23 Dec 2022, 15:19:05
    
    . 
    . sysuse auto.dta
    (1978 automobile data)
    
    . 
    . * Not that helpful in a log file
    . forvalues i = 0/1 {
      2.      regress mpg price if foreign == `i' 
      3. }
    
          Source |       SS           df       MS      Number of obs   =        52
    -------------+----------------------------------   F(1, 50)        =     17.05
           Model |  291.772809         1  291.772809   Prob > F        =    0.0001
        Residual |  855.669499        50    17.11339   R-squared       =    0.2543
    -------------+----------------------------------   Adj R-squared   =    0.2394
           Total |  1147.44231        51  22.4988688   Root MSE        =    4.1368
    
    ------------------------------------------------------------------------------
             mpg | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
    -------------+----------------------------------------------------------------
           price |  -.0007723    .000187    -4.13   0.000     -.001148   -.0003966
           _cons |   24.51661   1.272427    19.27   0.000     21.96086    27.07235
    ------------------------------------------------------------------------------
    
          Source |       SS           df       MS      Number of obs   =        22
    -------------+----------------------------------   F(1, 20)        =     13.25
           Model |  365.808088         1  365.808088   Prob > F        =    0.0016
        Residual |  552.055549        20  27.6027774   R-squared       =    0.3985
    -------------+----------------------------------   Adj R-squared   =    0.3685
           Total |  917.863636        21  43.7077922   Root MSE        =    5.2538
    
    ------------------------------------------------------------------------------
             mpg | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
    -------------+----------------------------------------------------------------
           price |  -.0015918   .0004373    -3.64   0.002     -.002504   -.0006797
           _cons |    34.9361   3.008144    11.61   0.000     28.66122    41.21097
    ------------------------------------------------------------------------------
    
    . 
    . * More helpful in a log file
    . regress mpg price if foreign == 0
    
          Source |       SS           df       MS      Number of obs   =        52
    -------------+----------------------------------   F(1, 50)        =     17.05
           Model |  291.772809         1  291.772809   Prob > F        =    0.0001
        Residual |  855.669499        50    17.11339   R-squared       =    0.2543
    -------------+----------------------------------   Adj R-squared   =    0.2394
           Total |  1147.44231        51  22.4988688   Root MSE        =    4.1368
    
    ------------------------------------------------------------------------------
             mpg | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
    -------------+----------------------------------------------------------------
           price |  -.0007723    .000187    -4.13   0.000     -.001148   -.0003966
           _cons |   24.51661   1.272427    19.27   0.000     21.96086    27.07235
    ------------------------------------------------------------------------------
    
    . regress mpg price if foreign == 1 
    
          Source |       SS           df       MS      Number of obs   =        22
    -------------+----------------------------------   F(1, 20)        =     13.25
           Model |  365.808088         1  365.808088   Prob > F        =    0.0016
        Residual |  552.055549        20  27.6027774   R-squared       =    0.3985
    -------------+----------------------------------   Adj R-squared   =    0.3685
           Total |  917.863636        21  43.7077922   Root MSE        =    5.2538
    
    ------------------------------------------------------------------------------
             mpg | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
    -------------+----------------------------------------------------------------
           price |  -.0015918   .0004373    -3.64   0.002     -.002504   -.0006797
           _cons |    34.9361   3.008144    11.61   0.000     28.66122    41.21097
    ------------------------------------------------------------------------------
    
    . 
    . log close example
          name:  example
           log:  example.txt
      log type:  text
     closed on:  23 Dec 2022, 15:19:05
    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

  • #2
    You can use -display- to show whatever you find suitable, e.g.

    Code:
    sysuse auto.dta
    
    
    forvalues i = 0/1 {
        display "regress mpg price if foreign == `i'"
        regress mpg price if foreign == `i'
    }

    Comment


    • #3
      Building on Joro's answer, you may find this helpful.
      Code:
      sysuse auto.dta
      capture program drop v
      program define v
          macro shift
          display _newline `". `0'"'
          `0'
      end
      forvalues i = 0/1 {
          v regress mpg price if foreign == `i' 
      }
      Code:
      . forvalues i = 0/1 {
        2.     v regress mpg price if foreign == `i' 
        3. }
      
      . regress mpg price if foreign == 0
      
            Source |       SS           df       MS      Number of obs   =        52
      -------------+----------------------------------   F(1, 50)        =     17.05
             Model |  291.772809         1  291.772809   Prob > F        =    0.0001
          Residual |  855.669499        50    17.11339   R-squared       =    0.2543
      -------------+----------------------------------   Adj R-squared   =    0.2394
             Total |  1147.44231        51  22.4988688   Root MSE        =    4.1368
      
      ------------------------------------------------------------------------------
               mpg | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
      -------------+----------------------------------------------------------------
             price |  -.0007723    .000187    -4.13   0.000     -.001148   -.0003966
             _cons |   24.51661   1.272427    19.27   0.000     21.96086    27.07235
      ------------------------------------------------------------------------------
      
      . regress mpg price if foreign == 1
      
            Source |       SS           df       MS      Number of obs   =        22
      -------------+----------------------------------   F(1, 20)        =     13.25
             Model |  365.808088         1  365.808088   Prob > F        =    0.0016
          Residual |  552.055549        20  27.6027774   R-squared       =    0.3985
      -------------+----------------------------------   Adj R-squared   =    0.3685
             Total |  917.863636        21  43.7077922   Root MSE        =    5.2538
      
      ------------------------------------------------------------------------------
               mpg | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
      -------------+----------------------------------------------------------------
             price |  -.0015918   .0004373    -3.64   0.002     -.002504   -.0006797
             _cons |    34.9361   3.008144    11.61   0.000     28.66122    41.21097
      ------------------------------------------------------------------------------
      
      .

      Comment


      • #4
        Thanks William and Joro! I should have mentioned I typically use display, but it becomes tedious with many commands that take place within a loop.

        Joro's solution:

        Code:
        sysuse auto.dta
        
        
        forvalues i = 0/1 {
            display "summarize mpg price if foreign == `i'"
            summarize mpg price if foreign == `i'
            display "regress mpg price if foreign == `i'"
            regress mpg price if foreign == `i'
        }

        William's clever solution saves lines, but requires prefixing each line with the new command.

        Code:
        forvalues i = 0/1 {
            v summarize mpg price if foreign == `i'
            v regress mpg price if foreign == `i'
        }
        William's may be the best solution for me, but I'm curious if there's a way to utilize "set trace" and "set tracedepth" so I don't have to prefix every command with another new command.

        Comment


        • #5
          I do not think that -set trace- would help here, because the expansion is in "the other direction," but I might be wrong...

          Here is another solution that does what you want, and we are not supposed to do such things because Stata Corp has discontinued the old looping construct -for-.

          Code:
          sysuse auto.dta
          
          for numlist 0/1:    regress mpg price if foreign == X
          results in the execution

          Code:
          . sysuse auto.dta
          (1978 automobile data)
          
          . 
          . 
          . for numlist 0/1:    regress mpg price if foreign == X
          
          ->  regress mpg price if foreign == 0
          
                Source |       SS           df       MS      Number of obs   =        52
          -------------+----------------------------------   F(1, 50)        =     17.05
                 Model |  291.772809         1  291.772809   Prob > F        =    0.0001
              Residual |  855.669499        50    17.11339   R-squared       =    0.2543
          -------------+----------------------------------   Adj R-squared   =    0.2394
                 Total |  1147.44231        51  22.4988688   Root MSE        =    4.1368
          
          ------------------------------------------------------------------------------
                   mpg | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
          -------------+----------------------------------------------------------------
                 price |  -.0007723    .000187    -4.13   0.000     -.001148   -.0003966
                 _cons |   24.51661   1.272427    19.27   0.000     21.96086    27.07235
          ------------------------------------------------------------------------------
          
          ->  regress mpg price if foreign == 1
          
                Source |       SS           df       MS      Number of obs   =        22
          -------------+----------------------------------   F(1, 20)        =     13.25
                 Model |  365.808088         1  365.808088   Prob > F        =    0.0016
              Residual |  552.055549        20  27.6027774   R-squared       =    0.3985
          -------------+----------------------------------   Adj R-squared   =    0.3685
                 Total |  917.863636        21  43.7077922   Root MSE        =    5.2538
          
          ------------------------------------------------------------------------------
                   mpg | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
          -------------+----------------------------------------------------------------
                 price |  -.0015918   .0004373    -3.64   0.002     -.002504   -.0006797
                 _cons |    34.9361   3.008144    11.61   0.000     28.66122    41.21097
          ------------------------------------------------------------------------------

          Comment


          • #6
            Joro,

            That's a neat trick! I'll have to think more carefully about my approach with loops. Thanks for entertaining my question! It's very helpful to know that there's not a straightforward built-in way to proceed for me. I appreciate it!

            Comment


            • #7
              Originally posted by Steven Beaker View Post
              Joro,

              That's a neat trick! I'll have to think more carefully about my approach with loops. Thanks for entertaining my question! It's very helpful to know that there's not a straightforward built-in way to proceed for me. I appreciate it!
              You are welcome, Steven.

              This sounds like a reasonable wish for Stata Corp, for them to offer that loops are executed noisily and somehow show the commands you are executing. The new loop constructs are in the core of Stata, the old -for- is an ado file.

              Code:
              . which for
              C:\Program Files\Stata17\ado\base\f\for.ado
              *! version 3.1.13  16feb2015
              
              . which foreach
              built-in command:  foreach
              
              . which forvalues
              built-in command:  forvalues

              Comment


              • #8
                Code:
                prog define code // to be traced
                
                sysuse auto.dta
                
                forvalues i = 0/1 {
                    
                    regress mpg price if foreign == `i'
                }
                
                end 
                
                set trace on
                set tracedepth 1
                
                log using "example.txt",   name(example) text replace
                
                code // to be traced
                
                log close example

                Comment


                • #9
                  #8 need set traceexpand on

                  Comment

                  Working...
                  X