Announcement

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

  • Generate year-variable

    I have a rather simple question:

    I have a date variable like "01/08/2007", how should the code look like if I want to drop all observations which are not observed in 2007?

    Alternatively how can I generate a new variable which only consist of the year from the date-variable above?

    Hope someone has an answer?

    /Jonas

  • #2
    If your date variable (bdate) is numeric (although displayed in date format):
    Code:
    generate byear = year(bdate)
    keep if byear==2007
    If the date variable (sdate) is string, you must create a numeric date variable first:
    Code:
    generate bdate = date(sdate,"DMY")         // or "MDY" if that is the sequence

    Comment


    • #3
      If that variable is a string, then

      Code:
       
      gen year = real(substr(sdate, -4, 4))
      should suffice. No need to create a daily date variable first.

      Comment


      • #4
        Thank you Svend

        Comment

        Working...
        X