Announcement

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

  • Extracting a string of digits from a numeric variable

    I have a variable that shows a date . I want to create a variable that shows the year i.e. to extract digits 6-9.
    The problem is that it is a numeric variable (double precision , %tc) although it looks like a string so one cannot use the substr command.
    I tried generating a string from it so as to use substr but that didn't work either.
    There must be some easy of doing this?

    1. | 06nov2021 00:00:00 |
    2. | 03oct2021 00:00:00 |
    3. | 10nov2021 00:00:00 |
    4. | 14oct2021 00:00:00 |
    5. | 09oct2021 00:00:00

  • #2
    Information on year is contained in this variable -- but not as a substring -- as the variable is numeric and contains number of milliseconds since the start of 1960.

    Code:
    gen year = year(dofc(whatever))
    See

    Code:
    help datetime

    Comment


    • #3
      This is a Stata date, so indeed it is not a string. So, forget everything you know about strings; it does not apply to your case.

      Fortunately, there are functions that allow you to extract parts of a date (assuming your variable is called date):

      Code:
      gen year = year(dofc(date))
      dofc() turns a Stata date-time variable in a Stata date variable, and year() extracts the year from a date. For more see help datetime
      ---------------------------------
      Maarten L. Buis
      University of Konstanz
      Department of history and sociology
      box 40
      78457 Konstanz
      Germany
      http://www.maartenbuis.nl
      ---------------------------------

      Comment


      • #4
        Thanks folks, sorted!

        Comment

        Working...
        X