Announcement

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

  • preserve versus frames

    I have a loop that works on one set of variables, prints a result, then iterates to work on another set of variables to give an unrelated result. Inside the loop it destroys part of the original data. I realise that I can now either preserve/restore for each iteration, or set up a frame within the loop for each iteration. Are there any technical advantages of one approach over the other? The code takes seconds to run, so I'm not worried about a minor time improvement.

  • #2
    In this situation it probably makes little difference. But frames can be very useful in other situations. See

    https://www.stata.com/manuals/dframesintro.pdf
    -------------------------------------------
    Richard Williams, Notre Dame Dept of Sociology
    StataNow Version: 19.5 MP (2 processor)

    EMAIL: [email protected]
    WWW: https://www3.nd.edu/~rwilliam

    Comment


    • #3
      Richard Williams is quite correct in pointing out that either -preserve/restore- or use of a -frame- could be used in O.P.'s situation and it makes little or no difference. In fact, in versions of Stata recent enough to support frames, preserve/restore is sometimes implemented as a "hidden" frame behind the scenes.

      Another approach is the use of -tempfile-s (which, behind the scenes, is how -preserve/restore- was implemented in pre-frame Stata versions, and sometimes still is.)

      Finally, the -runby- command, written by Robert Picard and me, available from SSC, may be another alternative. Although the main raison d'etre for -runby- is speed enhancement, it can also be used to just "get the rest of the data out of the way" to perform a task iteratively on blocks of data, which seems to be the goal in #1.

      Comment


      • #4
        Thanks. I went with preserve/restore which requires least program lines.

        Comment


        • #5
          More than once, I have spent ridiculous amounts of time trying to find the optimal way to do things when I could have quickly come up with something that was perfectly satisfactory.

          Frames are pretty useful though. It may be worth your time to learn more about them.
          -------------------------------------------
          Richard Williams, Notre Dame Dept of Sociology
          StataNow Version: 19.5 MP (2 processor)

          EMAIL: [email protected]
          WWW: https://www3.nd.edu/~rwilliam

          Comment

          Working...
          X