Announcement

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

  • do-file line-numbers to screen

    Dear All
    Does anyone know if it is possible to have Stata mirror the do-file line numbers to the screen when running the do-file (some line or the full file)/
    Regards
    Laurence

  • #2
    If you write your do-files with a good scriptable text editor, e.g. Vim, Textwrangler, BBEdit, you can add the line numbers as comments at the start of each line, e.g. /*1*/. Here's a quick Applescript that does it for BBEdit (or substitute "Textwrangler"):

    Code:
    tell application "BBEdit"
        select text 1 of text window 1
        add line numbers selection of text window 1 with adding space
        replace "^(\\d+)" using "/*\\1*/" searching in the selection of text window 1 options {search mode:grep, starting at top:true, wrap around:false, backwards:false, case sensitive:true, match words:false, extend selection:false}
    end tell
    Steve Samuels
    Statistical Consulting
    [email protected]

    Stata 14.2

    Comment


    • #3
      Laurence, you would be surprised, but no: this is not possible. Despite Stata is sharing it's roots with Fortran, C, and other respected programming languages it does not give you the line number where the error occurred.

      Steve made a suggestion to add the comments to the source. That will not work in case of ado files and programs in general, as the following example demonstrates:
      Click image for larger version

Name:	foobar.png
Views:	1
Size:	12.1 KB
ID:	127271

      Click image for larger version

Name:	foobar_list.png
Views:	1
Size:	1.4 KB
ID:	127273

      As you can see, all the comments were stripped out of the file and the program now only contains the executable code.

      On Friday Joe Canner and myself have requested better error reporting by Stata from the developers at the Stata Conference in Boston (it was a great event, really).

      For simple .do files you should be fine by setting tracedepth to 1 and trace to on.

      You can edit and print with line numbers from Stata's doeditor. Depending what problem you are solving there might be other workarounds. Why do you need line numbers?

      Best, Sergiy Radyakin

      Comment


      • #4
        Steve & Sergiy,

        Thanks for your replies. It's a pity it's not just a straightforward 'switch' in the do file. Steve's option takes me to places I just don't want to go. I don't think trace (or pause with display) helps in this case.

        Why do I want them? It sounds like I'm bordering on exceptionally lazy: I am running several long do-files operating on various years of survey data. Manipulations are around 1500 lines in each do-file. Screen output may not be what I expect -- if I see it as it whizzes by -- or an error is flagged. I then need to go the do-file and find the line of code -- which is usually using search on some text. It does sound lazy but doing this several time an hour highlights just how useful the line number in the do-file would be.

        Regards
        Laurence

        Comment


        • #5
          Laurence, if you only need line numbers in cases when an error occurs, that is to StataCorp.
          In most cases you don't need the exact line number - but something like a logical block - for that write the progress indication, such as done in this program.
          See discussion about interactively running a do-file just next door (next thread).
          Screen output may not be what I expect
          If you can formalize what you expect, assert it. Seriously. However if it is difficult, you can display a popup box and ask if it is ok to continue. In that way you will have time to look at your output.
          You can also ask only if the assert has failed, just in case you have many independent computations. Perhaps you still want to finish with the rest even if one has failed.
          Best, Sergiy

          Comment


          • #6
            Here is a small program that implements Steve's suggestion in Stata code. It uses the display command, rather than comments, to show the line numbers. As Sergiy pointed out, comments won't be displayed in the screen output.

            If you want to try this program, just copy this code to your do-editor. Then save the file as "dolines.ado". The file should be located either in your current working directory or in your personal ado directory. You can find the names of these directories with the commands -pwd- and -sysdir-. Finally, when you want to run your do-files, replace -do- with -dolines-, and include the .do suffix.

            dolines mydofile.do

            If you are looking for errors, though, you should look into the -assert- command, as Sergiy suggested. Looking for errors as the results wizz by is not always reliable.

            Mike

            Code:
            program dolines
                args infilename
                tempfile outfilename
                
                tempname infh
                tempname outfh
                
                file open `infh' using `"`infilename'"', read
                file open `outfh' using `"`outfilename'"', write
                
                local linenum = 0
                file read `infh' line
                while r(eof)==0 {
                    local linenum = `linenum' + 1
                    
                    file write `outfh' `"display as text "line number: " as result "`linenum'" "' _n
                    file write `outfh' `"`macval(line)'"' _n
                    
                    file read `infh' line
                }
                file close `infh'
                file close `outfh'
                
                do `"`outfilename'"'
            end

            Comment


            • #7
              Dear All
              Many thanks - you have given me enough to be able to figure out how to do as I need (in this case and others to follow).
              Regards
              Laurence

              Comment


              • #8
                Postscript. Perhaps this may help remind those newer to Stata of the care that needs to be taken with the 'assert' (as noted in several places by others).
                I used the suggested stopbox with assert from helpful list members and came up with this (where X1 & X2 are monetary values derived from different methods that should be equal -- but differ by fractions of cents due, I believe, to the way Stata stores and manipulated numerical values):

                Code:
                assert abs(X2-X1)<0.001
                window stopbox rusure "Difference is less than 1/10 cent" ///
                 "Do you want to continue? " "`char(13)' Y for Continue" " N for stop here"
                window stopbox note "Continued by request"
                Regards
                Laurence

                Comment

                Working...
                X