Announcement

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

  • Dates in stata

    Hi all,

    I am working on a trial that includes primary care data from GP practices (32 practices). However, I have an issue with dropping some observations (rows). At this stage, each patient has multiple rows and I will reshape the data once I drop irrelevant data.

    Practices provided data that goes back to 20-30 years back so I need to drop all observations that are before the randomisation date for each practice.

    For example, v3 is the appointment date which is in string format. The format of the dates is inconsistent, e.g. some appointment dates are recorded as 1942 or 20-May-1998 (in 20th century) or 19-April-2012 or 17-Aug-2022 (in the 21st century) and so on.

    How can I format the dates so all have the same format (MDY) and also include appointments that are after the randomisation date e.g. 23 April 2022.

    Kind regards,

  • #2
    The daily dates you mention all yield easily to date() or daily().

    Year-only dates might be fixed with a notional day within year but otherwise flagged.

    The intentional garbage entry here is a placeholder to flag that other rules might be needed.

    Code:
    clear 
    input str42 date
    "1942"
    "20-May-1998"
    "19-April-2012"
    "17-Aug-2022"
    "garbage"
    end 
    
    gen wanted = daily(date, "DMY")
    gen year_only = inrange(real(date), 1900, 2023)
    replace wanted = mdy(7,1, real(date)) if inrange(real(date), 1900, 2023) 
    format wanted %td 
    
    list 
    
    
         +--------------------------------------+
         |          date      wanted   year_o~y |
         |--------------------------------------|
      1. |          1942   01jul1942          1 |
      2. |   20-May-1998   20may1998          0 |
      3. | 19-April-2012   19apr2012          0 |
      4. |   17-Aug-2022   17aug2022          0 |
      5. |       garbage           .          0 |
         +--------------------------------------+

    Comment

    Working...
    X