Announcement

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

  • preserve and restore commands

    Hi, I am trying to execute the simple code from Christopher Baum's book, "An Introduction to STATA Programming."

    sysuse auto, clear

    generate lprice = log(price)

    preserve

    collapse (max) max_lprice=lprice max_mpg=mpg (iqr) iqr_lprice=lprice iqr_mpg=mpg if !missing(rep78), by(rep78)

    sort rep78

    save repstates, replace

    restore

    However, it seems that the preserve/restore command is not working, as I get the following message:

    nothing to restore
    r(622);

    I will appreciate any help.

    Ahmed

  • #2
    My best guess is that you are running this code from a do-file, not from the command window, and that you are doing it line-by-line or in chunks. When -preserve- is run from a do file, the -preserve-d file is maintained only until the end of the code that was executed from the do-file with it. So if execution was interrupted somewhere between the -preserve- and the -restore-, as soon as that interruption is reached, the -preserve-d file is automatically -restore-d at that moment. So when you later separately execute the -restore- command, there is nothing to restore anymore.

    If you are running this from a do-file, then you must run the entire thing, beginning to end, without interruption. Alternatively, you can do it line-by-line from the Command window. But you can't run it in pieces from the do-file.

    Comment


    • #3

      Thank you so much for your prompt response. I have followed your advice and the code is working now. Thank you again.

      Ijaz



      Last edited by Ijaz Ahmad; 26 Jan 2023, 12:11.

      Comment

      Working...
      X