Announcement

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

  • Generate a variable2 by modifying the dataset but then restore the dataset along with var2. preserve and restore doesnt keep var2

    I want to use preserve and make edits/modifications to the dataset and in the process a column variable2 is created using this modified dataset. Now I want to restore the original dataset but also have this variable2 column. What can I do? The only thing I can imagine is saving the old dataset rather than using preserve, then saving the modified dataset with the variable2 and then finally merging them where I keep only variable2. But is there an easier and better way to do this? I imagine Stata or someone might have created a package because this seems to a very general use algorithm.
    Thanks

  • #2
    When you -restore- a -preserve-d data set, you get back what you had immediately before the -preserve-. So if variable2 didn't exist before -preserve-, then, of course, it will be lost after -restore-.

    If you are using version 16 or later, you can do what you want using -frames-, and it will be more efficient. The scheme would be like this:

    Code:
    use original_data_set
    gen `c(obs_t)' obs_no = _n // CREATE AN OBSERVATION IDENTIFIER
    
    frame copy default modified
    frame modified {
        // CODE TO CREATE variable2 HERE
    }
    frlink 1:1 obs_no, frame(modified)
    frget variable2, from(modified)
    drop modified
    frame drop modified
    This is more efficient than saving and merging because it is all done in memory without involving any disk operations.

    Added: If the original data set already contains a variable, or set of variables, that uniquely identifies observations, then there is no need to create the obs_no variable. Just use the uniquely identifying variable(s) as the linking variable(s) in the -frlink- command instead.

    Comment

    Working...
    X