Announcement

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

  • generating new variable based on date variable

    Dear Stata community,

    I have a variable stored as dates in the Stata format i.e 01jul1993 (I converted them from string into the correct format). However
    I know want to create a new variable which organises the observations according to whether the date was pre or post 31dec1996. what would be the best command to use please? I have tried reading the manual in reference to dates/ times but I could find anything that helps with generating new variable according to the date.

    Many thanks for your help,

    Roshani

  • #2
    Try

    Code:
    gen pre31dec1996 = date<td(31dec1996)

    Comment


    • #3
      This example shows that true or false comparisons yield 1 if true and 0 if false.


      Code:
      . di mdy(10, 6, 2020) > mdy(12, 31, 1996)
      1
      
      . di mdy(10, 6, 1920) > mdy(12, 31, 1996)
      0
      which leads to a guess that you want something like

      Code:
      gen wanted = mydate > mdy(12,31,1996)
      with caution for missing values as

      Code:
      gen wanted = mydate > mdy(12,31,1996) if mydate < .
      and also thought about whether > should be >=.


      You can also use the td() function. I just use mdy() because I can remember it and its exact syntax.

      Comment


      • #4
        Thank you Nick and Joro, I'll give this a try. Bw Roshani

        Comment

        Working...
        X