Announcement

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

  • Splitting strings

    I have a string variable containing licence plates, names and dates separated by / . As in 122000200106_Cheyrmen, Murauwatpur Panchayat Pacs-02/11 where 11 is 2011, in 121900200198_Shiv Nath Prasad Sah-72/2007 2007 is 2007, and in 121500100430_Dinesh Poddar-02/93 93 is 1993. How can I extract the year in YYYY format? Any suggestions would help, thanks


  • #2





    Please read and act on FAQ Advice #12 https://www.statalist.org/forums/help#stata to give well-formatted data examples. Otherwise with some rocket surgery I get this and now suggest some technique. In general


    Code:
    help string functions
    tells you about helpful tools.

    Code:
    clear
    input str100 whatever
    "122000200106_Cheyrmen, Murauwatpur Panchayat Pacs-02/11"
    "121900200198_Shiv Nath Prasad Sah-72/2007"
    "121500100430_Dinesh Poddar-02/93"
    end
    
    gen year = real(substr(whatever, strrpos(whatever, "/") + 1, .))
    
    replace year = cond(year < 25, 2000 + year, cond(year < 100, 1900 + year, year))
    
    list year  
    
         +------+
         | year |
         |------|
      1. | 2011 |
      2. | 2007 |
      3. | 1993 |
         +------+

    Comment


    • #3
      Thanks for your help, the code worked. And, I apologize for the inconvenience in the posted example. I will make sure to provide formatted examples in the future.

      Comment

      Working...
      X