Announcement

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

  • Preserve, Restore and Reshape

    Hello,

    I have a strange problem with a foreach loop with a reshape command which looks like this:

    Code:
    levelsof id_ne, local(levels)
    foreach l of local levels {
    preserve
    
    drop if id_ne != `l'
    keep raeume time_stamp_trunc persgradtagzahl
    
    reshape wide persgradtagzahl, i(time_stamp_trunc) j(raeume)
    
    save `path_temp'tempfile`l'.dta, replace
    
    restore
    }
    The first round of the loop works perfectly fine. When ending the first loop, the data is restored. After that (only after the first round), the code seems to skip the drop and keep command which leads stata to not beeing able to conduct the reshape command.

    Any ideas?
    Last edited by Katja Lepper; 28 Jul 2017, 08:06.

  • #2
    1. How do you know the drop+keep are skipped? Note that if reshape fails, since you run an earlier reshape the data you'll see is how it was when you ran preserve (i.e. before the two commands were executed)

    2. You don't really need to preserve *every loop* if you use the "restore, preserve" option. Something like:

    Code:
    preserve
    for .. {
    keep + drop + save
    restore, preserve
    }

    Comment

    Working...
    X