Announcement

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

  • Execute certain lines of a Do file

    Hi everyone. Is there any code to execute only certain lines of a do file? I know you can execute the whole file by "do namefile.do". I appreciate. Joan

  • #2
    You can do that interactively from the do-file editor. Or you can split your do-file into smaller files. But watch out: any definitions of local macros are only visible within the same chunk of code.

    Comment


    • #3
      By "interactively", you mean selecting the lines? I meant a "code". It would be very nice to write something like do dofile.do 14-19 So as to execute only lines 14-19

      Thank you Nick

      Comment


      • #4
        That is programmable, but I am not aware of a program to do it. I have to say that in my view it's a bad idea.

        1. There are better ways to work.

        If you want to do that repeatedly, just put the lines in a separate file. If you want to do it even once, putting the lines in a separate file is about as much work as working out the syntax.

        2. This makes your code more cryptic and is a step away from comprehensible, reproducible research. Somebody else has to count the lines or look at the file to understand what you did. In a week's time, or in a month's time, that somebody else could be you.

        3. Even being off by one line could lead to puzzling bugs and a need to repeat the code. The idea would not be a time-saver then.

        4. If you're editing the do-file (and who doesn't), the line references are likely to be broken.

        5. Executing parts of do-files can scupper references to local macros, behind many puzzled posts here.

        Comment


        • #5
          One thing you could do is use the fact that you can add arguments to the line do dofile. These are stored in the local macros `1', `2', `3', etc. So you can create a .do file like this:

          Code:
          if "`1'" == "foo" {
              di "this works"
          }
          if "`1'" == "bar" {
              di "this too"
          }
          and then do do foobar foo or do foobar bar, to run either block within the do-file foobar.do.
          Last edited by Maarten Buis; 24 Nov 2015, 07:38.
          ---------------------------------
          Maarten L. Buis
          University of Konstanz
          Department of history and sociology
          box 40
          78457 Konstanz
          Germany
          http://www.maartenbuis.nl
          ---------------------------------

          Comment

          Working...
          X