Announcement

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

  • How to extract a certain portion of a string variable?

    Hello

    I have a %tc variable (PERIOD_END) for date:

    Code:
    31mar2014 00:00:00
    28feb2014 00:00:00
    There are numerous possibilities for the date, but in each case the year makes up characters 6-9 of the variable. I tried to extract only these characters using the following code:

    Code:
    gen year = substr("PERIOD_END",6,9)
    That only assigned the characters "D_EN" to a variable called year. I also unsuccessfully tried the regexm command:

    [code]gen year=2014 if regexm("PERIOD_END", "2014") == 1
    [/code

    I have a feeling I'm supposed to use the regexm command, but that I am using it incorrectly. Thanks.
    Last edited by Greg Saldutte; 01 Jul 2019, 12:58.

  • #2
    first, by using quotes, you instructed Stata to use the substr function on that string; second, the last element of the command (where you have "9") is the length of the substr, not the end point; so, just modify your command as follows:
    Code:
    gen year = substr(PERIOD_END,6,4)

    Comment


    • #3
      Thank you. I tried your code, but I get the error of "type mismatch."

      I used the command "describe PERIOD_END," which revealed that it is a %tc variable. Perhaps that will make it easier to find out why there is a type mismatch.

      Comment


      • #4
        You want something like:

        Code:
        gen year = year(dofc(PERIOD_END)))
        Your data is actually an integer (milliseconds since 01jan1960 00:00:00.000, assuming 86,400 sec/day) that has a display format that makes it look like a string. But it is not a string, so string functions won't work on it. Code above extracts the date from the timestamp, and then pulls out the year from the date.
        Last edited by Dimitriy V. Masterov; 01 Jul 2019, 13:03.

        Comment


        • #5
          That worked perfectly. Thank you.

          Comment

          Working...
          X