Announcement

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

  • Generate new variables from dates

    Hi everyone!
    I am new to STATA and I need help to generate a new variable (actually two, but I assume I need to do this in two steps) based on three existing variables. Two of the excising variables are dates.
    The current variables are 'event' (event at any timepoint, binary), 'date_of_event' and 'date_of_inclusion'.
    I wish to divide the 'event' variable into two new variables whether or not the event happened before or after inclusion into a cohort.

    The following codes put every observation from 'event' in the 'event_before' and none in the 'event_after', and I can't seem to fix it.

    *string dates converted to STATA dates
    generate stata_eventdate = date(date_of_event, "DMY")
    format stata_eventdate %td
    generate stata_inclusiondate = date(date_of_inclusion, "DMY")
    format stata_inclusiondate %td

    generate event_before = 1 if event ==1 & stata_eventdate <= stata_inclusiondate
    replace event_before = 0 if missing(event_before)

    generate event_after = 1 if event ==1 & stata_eventdate > stata_inclusiondate
    replace event_after = 0 if missing(event_after)


    Thank you so much!

  • #2
    Very often a simple description really isn't clear without more detail, or at a minimum it is too difficult to guess at a good answer from what has been shared.

    Please help us help you. Show example data. Show your code and show us what Stata told you. The Statalist FAQ provides advice on effectively posing your questions, posting data, and sharing Stata output. It is particularly helpful to copy commands and output from your Stata Results window and paste them into your Statalist post using code delimiters [CODE] and [/CODE], and to use the dataex command to provide sample data, as described in section 12 of the FAQ.

    Comment


    • #3
      .
      Last edited by Salvatore Viola; 04 Oct 2022, 10:31. Reason: Deleted for redundancy

      Comment

      Working...
      X