Announcement

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

  • Converting string into integer

    Dear All,

    I know this subject has appeared on the forum quite often. However, I didn't find a solution to my problem.
    I importer data from xlsm. I have monthly date in string like 12.2021. I wanna obtain a year from it.
    I used a command destring date, gen(date_1).
    However, it isn't what I want. E.g. for 12.2010 I obtained 12.201.
    When I typed gen year=yofd(date_1)
    I got 1960 for 2009
    Please help me. I'm out of the ideas. I tried encode, real, recast but I didn't obtain what I wanted

  • #2
    Hi Guest,

    if you know that all your dates have the same format (i.e. MM.YYYY), you can simply create a new variable that is equal to the last 4 characters of your original string-date variable. The easiest way of doing this is with substr().
    Last edited by sladmin; 28 Aug 2023, 09:38. Reason: anonymize original poster

    Comment


    • #3
      Thank you Ulrich. I typed gen year=substr(date,4,4). I finally obtained what I wanted. Thank you so much!

      Comment


      • #4
        You've got a good solution, but let's explain why there were problems with what you tried.

        "12.2010" is for destring just 12.2010 as a string. That is fine as a number, 12 plus a fraction. Most display formats will elide the last 0.

        Applying yofd() says to Stata that 12.2010 is a daily date -- because yofd() is a function to extract years from daily dates. It is not a general function to extract years from any kind of date.

        Code:
        . di %td 12.201
        13jan1960
        
        . di %td 12
        13jan1960
        Stata's rules here say to ignore the fractional part but otherwise12 is an acceptable daily date -- in 1960, given that 0 is 1 January 1960 -- and that is the year returned.

        https://www.stata-journal.com/articl...article=dm0098 is a round-up of advice which flags early that dates need special date functions, usually. Here real(substr()) does do what you want. Note that I pushed the result through real() as the string alone is of less use.

        Another approach would be to get Stata to treat your variable as the monthly date it surely is.

        Code:
        . di yofd(dofm(monthly("12.2010", "MY")))
        2010

        Comment


        • #5
          Thank you so much for such a detailed explanation!

          Comment

          Working...
          X