Announcement

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

  • Preserve & restore in loop

    Hi there,

    I'm having some problems using the preserve and restore functions in a loop.

    When I run the code below the code block for sex works fine - I get excel spreadsheets for both categories of sex.

    However, the second block of code (for age) works for the first level(category) of age (& produces an excel spreadsheet) but when the loop goes to run for the second time I get the error "no observations r(2000);".

    What am I doing wrong?

    Thank you in advance!

    Mel


    Code:
    *Sex
    
    cd "xxx"
    use xxx.dta, clear
    
    levelsof sex, local(levels)
    foreach lev of local levels{
    preserve
    display "sex = `lev'" 
    tabstat sleep_dur_tue_before sleep_dur_wed_before sleep_dur_thur_before sleep_dur_fri_before sleep_dur_sat_before sleep_dur_sun_after sleep_dur_mon_after sleep_dur_tue_after sleep_dur_wed_after sleep_dur_thur_after if sex == `lev', stats(mean sem) save 
    return list
    matrix spring_matrix_sex_`lev' = r(StatTotal)      
    matrix list spring_matrix_sex_`lev'
    svmat spring_matrix_sex_`lev'
    save "spring_matrix_sex_`lev'.dta", replace
    keep spring_matrix_sex_`lev'*
    drop if spring_matrix_sex_`lev'1 ==.
    export excel spring_matrix_sex_`lev'.xlsx, firstrow(varlabels) replace
    restore
    }
    
    *Age
    cd "xxx"
    use xxx.dta, clear
    
    levelsof age_accel_tert, local(levels)
    foreach lev of local levels{
    preserve
    display "age = `lev'" 
    tabstat sleep_dur_tue_before sleep_dur_wed_before sleep_dur_thur_before sleep_dur_fri_before sleep_dur_sat_before sleep_dur_sun_after sleep_dur_mon_after sleep_dur_tue_after sleep_dur_wed_after sleep_dur_thur_after if sex == `lev', stats(mean sem) save 
    return list
    matrix spring_matrix_age_`lev' = r(StatTotal)      
    matrix list spring_matrix_age_`lev'
    svmat spring_matrix_age_`lev'
    save "spring_matrix_age_`lev'.dta", replace
    keep spring_matrix_age_`lev'*
    drop if spring_matrix_age_`lev'1 ==.
    export excel spring_matrix_age_`lev'.xlsx, firstrow(varlabels) replace
    restore
    }

  • #2
    Well, it seems odd that in a loop that iterates over levels of an age variable, the calculations in side the loop are conditioned on -if sex == `lev'-. Probably there is no value of the sex variable corresponding to the second level of the age variable. Also it doesn't really make sense. I'm guessing you meant -if age_accel_tert == `lev'-.

    Comment


    • #3
      Clyde has found the crucial error, but here are some other things you might find useful:

      1) You didn't need to -preserve- inside the loop. You only need to preserve just once, before the loop. Notice that you -restore- at the bottom of the loop, so your -preserve- command at the top of your loop is just preserving (i.e., temporarily saving a copy of) your original data each time.

      2) I'd suspect you don't need -preserve/restore- at all or any of all the drop/keep business. I don't fully understand what you want to do, but I think you could use -export excel- with a varlist of the variables you want to save each time and an -if- qualifier. Something like this might work:

      Code:
      export excel spring_matrix_age_`lev'* using spring_matrix_age_`lev'.xlsx if (spring_matrix_age_`lev'1 !=.), firstrow(varlabels) replace

      Comment


      • #4
        Thank you both very much! I so busy convincing myself I didn't know how to use preserve & restore that I completely missed the basic mistake! And thanks Mike - I'll have a play round with what you've suggested.

        Comment

        Working...
        X