Announcement

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

  • Combine two time and date variables, and creating a yearly (readable) variable

    Dear stata community

    I have a data-set that is combined from two different datasets, by append. I have a variable for time and date in each of the original datasets, and now I have two variables with time and date. How can I combine these (they have the same format)? And then I want to make a new variable that shows only the year, so I can sort observations within a patientID, on year.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str9 PatientID float(dato2 date2)
    "1" 19981     .
    "1" 20394     .
    "2" 20922     .
    "2"     . 20149
    "2"     . 19308
    "3"     . 20579
    "3" 19756     .
    "4" 20579     .
    "4"     . 19471
    "4" 19797     .
    end
    format %tdCCYY-NN-DD dato2
    format %tdCCYY-NN-DD date2

    Cathrine

  • #2
    Code:
    gen date = cond(mi(dato2), date2, dato2)
    format date %tdCCYY-NN-DD
    gen year = yofd(date)
    It would be better to unify variable names before appending them.
    Last edited by Fei Wang; 06 Dec 2021, 04:54.

    Comment


    • #3
      Code:
        
       gen date = min(date2, dato2)
      would also work fine with this example.

      A paranoid check for two non-missing values could be


      Code:
      list data?2  if !missing(dato2, date2)


      Comment


      • #4
        Thank you so much! I also unified the variables before appending them, so that was helpful.

        Comment

        Working...
        X