Announcement

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

  • Splitting Variable after fourth character

    Hi all,

    I have the following table where I want to split the variable "id". This variable always consists of four numbers followed by the company name. Therefore, I want to split this variable into two variables "year" and "company". "Year" should include the first four numbers of the "id" variable and "company" the characters following the first four numbers. Hence, I want to split the entries in the column "id" after the fourth character. Yet, the column "id" shall not be eliminated but remain within the table.

    Example: "20073COM CORP" should be split up into "2007" and "3COM CORP"..

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long id float(RemainingTenure FEMALE) byte _merge
     1 2.4285715         0 3
     2         2        .3 3
     3         2         0 3
     4         2 .11111111 3
     5         2        .2 3
     6         2        .1 3
     7         3         0 3
     8         2         0 3
     9         2         0 3
    10         2 .15384616 3
    end
    label values id id
    label def id 1 "20073COM CORP.", modify
    label def id 2 "20073M CO", modify
    label def id 3 "20074KIDS ENTERTAINMENT, INC.", modify
    label def id 4 "200799 (CENTS) ONLY STORES", modify
    label def id 5 "2007A. M. CASTLE & CO.", modify
    label def id 6 "2007A. O. SMITH CORP.", modify
    label def id 7 "2007AAR CORP.", modify
    label def id 8 "2007AARON RENTS, INC.", modify
    label def id 9 "2007ABAXIS, INC.", modify
    label def id 10 "2007ABBOTT LABORATORIES", modify
    label values _merge _merge
    label def _merge 3 "matched (3)", modify
    Thanks for your support.

    Best

    Matthias

  • #2
    Thanks for the data example. I believe this does what you want:
    Code:
    decode id, gen(id_str)
    gen year = real(substr(id_str, 1, 4))
    gen company = substr(id_str, 5, .)

    Comment


    • #3
      Thank you for your help Wouter, this is exactly the command I needed.

      Comment

      Working...
      X