Announcement

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

  • Adding Observations with a Fixed Time Value

    This is a sort of revision of my previous post:

    https://www.statalist.org/forums/for...-time-variable

    I figured this title was more descriptive of my issue and more open to different ways to solve it. To sum up that post, I need to add an observation with a precise Time value (which is in seconds for my dataset) for Time = 60, 120, 180, 240, and 300. I want the other variables in the new observations I create to match the last observation closest to the new time one. For example, I may have an observation at t=57.8 seconds and my next one is t=67.2 seconds. I want to add an observation for t=60 seconds and have all other variables in that observation match that of the t=57.8 observation.

    Thank you so much in advance!

    Nicole

  • #2
    So something like this:

    Code:
    //  CREATE A DATA SET WITH JUST DESIRED VALUES OF
    //  TIME, AND NOTHING ELSE
    use original_data, clear
    keep section
    duplicates drop
    expand 5
    by section, sort: gen time = 60*_n
    gen byte added_obs = 1
    
    //  NOW BRING IN THE ORIGINAL DATA
    append using original_data
    replace added_obs = 0 if missing(added_obs)
    
    //  SORT OBSERVATIONS ON TIME AND COPY
    //  CLOSEST OBSERVATION BEFORE
    ds time added_obs, not
    foreach v of varlist `r(varlist)' {
        by section time, sort: replace `v' = `v'[_n-1] if added_obs
    }
    Not tested. Beware of typos.

    Note: In providing this code I am not endorsing the wisdom of this approach. My instinct is that interpolation or some other approach might be more sensible than carrying forward, but that is a scientific issue that you and your collaborators would understand best.

    Comment

    Working...
    X