Announcement

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

  • extract date out of a string Variable

    hey,
    I´ve the Variable date_ideenwettbewerb
    Code:
                  storage   display    value
    variable name   type    format     label      variable label
    -----------------------------------------------------------------------------------------------------------------------------------
    date_Ideenwet~b str8    %9s
    Which describes the time of an Event, but not in date Format it´s written like 2015-I for the first Event in 2015 or 2012-II for the second Event in 2012.
    I´ve two question, at first I would like to know whether it is possible to extract the year, so that I would have at least a variable which signals the year of the Event.
    Or whether it would be possible to generate a date like 1.1.2015 for 2015-1 or 1.4-2012 for 2012-II.
    Any help would be great, alos if you need more Information I´ll provide them!
    Thanks

  • #2
    The year should be just be

    Code:
     
    gen year = real(substr(date_ideenwettbewerb, 1, 4))
    as it is just the first four characters converted into a numeric value.

    The serial identifier within years can be extracted similarly as whatever follows the hyphen

    Code:
     
    gen id = substr(date_ideenwettbewerb, strpos(date_ideenwettbewerb, "-") + 1, .)
    This kind of thing is much easier than it may seem. We find where the hyphen is, start at the next character and then extract the rest. In fact, if your data are well behaved you could just start at position 6.

    For more introductory and expository on getting to know functions, see http://www.stata-journal.com/article...article=dm0058

    From your examples, the identifier is a Roman numeral I, II, ... and so what springs to mind is http://www.stata-journal.com/article...article=dm0054

    I can't see that the information you have justifies turning that information into a date within the year.



    Comment


    • #3
      Thanks!
      Code:
      substr
      solved my Problem!

      Comment

      Working...
      X