Announcement

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

  • Error 622 with preserve/restore

    I have a feeling I must be making a basic error here, but each time I try to use preserve and restore, I get the error "nothing to restore" r(622), for which the manual says:

    "error . . . . . . . . . . . . . . . . . . . . . . . . Return code 622
    nothing to restore
    You issued the restore command, but you have not previously specified
    preserve."
    This code reproduces the problem:

    drop _all
    set obs 30

    gen x = rbinomial(1, 0.5)
    gen y = rnormal() + x

    list

    preserve

    collapse y, by(x)
    list

    restore
    What am I doing wrong, please? The following does work, so is the problem something to do with my generating data from scratch?

    webuse college
    preserve
    list in 1/10
    collapse (mean) gpa, by(year)
    list
    restore
    I'm using Stata/BE 18.0 for Mac (Intel 64-bit), revision 04 Sep 2024. MacOS version is 14.6.1, Intel i5. Thank you.
    Last edited by Kate OHara; 29 Oct 2024, 08:13.

  • #2
    This will happen if you execute some commands interactively by typing them into Stata directly, and execute others from a do-file. For most commands, this causes no issues. For others, especially macros and preserve/restore, this does. This is because when you execute code from a do-file, it is run inside its own local scope, and preserve/restore operates with the same concept. They cannot "see" the effect of the other when run in these two different ways.

    To fix it, ether run the entire block of code from a do-file, or else work exclusively in interactive mode until you are ready to move your code into a do-file.

    Comment


    • #3
      Thank you Leonardo, that's brilliant. I understand and it fixes the problem.

      Comment

      Working...
      X