Announcement

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

  • finding the earliest minimum date across dates in several variables

    I have a dataset which contains observations of 1000 subjects. In this dataset, I have 4 variables in date format for each woman. lets call them var1, var2, var3, var4. I want to create a new variable, and that variable should be the earliest date (either var 2, var 3, or var 4) after the date of var 1. That is, stata should pick the next closest date after the date in var1 (the dates should be picked from either var 2 or var 3 or var 4). How can I do this please?
    Last edited by sladmin; 30 May 2024, 11:57. Reason: anonymize original poster

  • #2
    You can work with

    Code:
    gen first = min(var2, var3, var4)
    If that is before var1 you need something more complicated. Not tested and quite possibly even it is correct there is a better way to do it.

    Code:
    gen wanted = var2 if var2 > var1 
    replace wanted = var3 if var3 < wanted & var3 > var1 
    replace wanted = var4 if var4 < wanted & var4 > var1

    Comment


    • #3
      This is soo amazing!!! Thank you so much. Code 2 is what I was looking for and is what worked.

      Comment

      Working...
      X