Announcement

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

  • Simple wrapper for set trace

    Edit: After some testing, it appears to be a scope issue; set trace on applies only to the context of the wrapper program. So, is there any way to make the effect of set trace apply globally?

    I am attempting to write wrapper programs that allow me to type ston and stoff instead of set trace on and set trace off. I am using Stata 16 for unix. I have created the following files:
    ston.ado:
    Code:
    program ston
        version 16
        set trace on
    end
    stoff.ado:
    Code:
    program stoff
        version 16
        set trace off
    end
    However, these do not have the desired effect. Starting with trace set to off, I can issue the command ston as many times as I like and no output is produced. If I instead set trace on[/CODE], then I can call either ston or stoff as many times as I like and I get the output:
    Code:
    ------------------------------------------------------------------------------------------------------------ begin ston ---
    - version 16
    - set trace on
    -------------------------------------------------------------------------------------------------------------- end ston ---
    or
    Code:
    ----------------------------------------------------------------------------------------------------------- begin stoff ---
    - version 16
    - set trace off
    ------------------------------------------------------------------------------------------------------------- end stoff ---
    every time. [Edit: So the programs are acknowledged and executed but with no apparent effect, at least with respect to the command window where I am typing interactively. Could it be a scope issue? That would surprise me.]
    I have searched for answers about simple wrappers and wrappers for set trace to no avail. Why doesn't this work as expected? Is there a better solution?
    Last edited by Michael Harris differentiated; 01 Nov 2019, 08:33. Reason: Identified as scope issue; updated to ask for workaround

  • #2
    See sto and tr both from SSC.

    Best
    Daniel

    Comment


    • #3
      Daniel Klein documents my sto package on SSC with identical intent. It was broken utterly by a change of the form you fear in a new release of Stata, perhaps two or three versions ago.

      Comment


      • #4
        Thanks for those references. The behavior I want is closest to sto, and I was able to get something close by putting the commands in a local macro like
        Code:
        loc ston set trace on
        loc stoff set trace off
        and then typing `ston' and `stoff' as needed.
        A final request: I'd like to be able to type `sto' instead as a toggle. Can I access the state (on or off) of
        trace somehow, for making the local macro store an if command?
        Last edited by Michael Harris differentiated; 01 Nov 2019, 09:27.

        Comment


        • #5
          I see how to do it from Nick's sto code from SSC. The command local onoff : set trace is wonderful. Where is that kind of command documented? I do not see anything about : set trace in the documentation for macro in the Programming Reference Manual.

          Comment


          • #6
            My solution is
            Code:
            loc sto set trace \`=cond("\`: set trace'" == "on", "off", "on")'
            which is messy but works. It delays substitution so the status of trace can be determined interactively.
            The command to invoke the toggle is `sto'.
            I put the line in a separate file and then used
            include, but it could also be pasted in a do-file directly.

            Comment


            • #7
              Nick wrote that quite a while ago. My guess is that this was once documented. In modern Stata, type

              Code:
              display c(trace)
              and

              Code:
              help creturn
              for more information.

              Best
              Daniel

              Comment


              • #8
                Why do you want this in the first place?

                If you are looking for a quick way to set trace on and off, try

                Code:
                global F4 set trace on;
                global F5 set trace off;
                You can then set trace on and off by pressing the F4 and F5 buttons, respectively.

                Best
                Daniel

                Comment


                • #9
                  Thank you, Daniel. I had forgotten that trace information is stored in the c-class. I also was unaware that I could assign macros to the keyboard. I created a toggle key by combining your #8 and my #6.
                  Last edited by Michael Harris differentiated; 01 Nov 2019, 10:24.

                  Comment

                  Working...
                  X