Announcement

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

  • keep the row of information which is closest (in days) to a selected date variable

    I would like to keep one row of information for each ID which has the

    preop_date or recpreop_date CLOSEST to the surgery date

    Please note I've posted this as a continuation of another thread, but I thought as this is another question sepeate to the original post I thought I would post separately

    Code:
    //Select which is the correct pre_opdate (from the received vs preopdate) by finding the one closest to the operationdate //Generates the days from surgerydate  
    
    gen preop_correct1 = abs(operationdate-preop_date)  
    gen preop_correct2 = abs(operationdate-recpreop_date)
    
     //I then don;t know how to proceed from here

    Dataset:



    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float id str2 id2 float(pain scar surgdate oks gender preop_date recpreop_date postop_date recpostop_date operationdate) byte(_merge duplicatescores)
    12 "1A" 1 1 22555 40 1 22533 22289 22770 22771 22555 3 0
    13 "2B" 1 9 11962  1 2 11933 11933 11963 11782 11962 3 0
    13 "2B" 1 9 11962 48 2 11932 11951 11963 11782 11962 3 1
    13 "2B" 1 9 11962 48 2 11932     .     .     .     . . 1
    end
    format %td surgdate
    format %td preop_date
    format %td recpreop_date
    format %td postop_date
    format %td recpostop_date
    format %td operationdate
    label values _merge _merge
    label def _merge 3 "Matched (3)", modify

  • #2
    There is a rowmin egen function you can use like this below. But you will have to have more assertion statements to make sure there are no missing values and differences are actually positives etc..Perhaps other expert Statalisters will nest this whole thing below is a single conditional statement.

    Code:
    egen wanted = rowmin(preop_correct1 preop_correct2)
    bys id (wanted): keep if _n == [1] 
    drop wanted preop_c*

    Comment


    • #3
      Nice and simple ! Great one, was playing around with reshaping !

      Comment

      Working...
      X