Announcement

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

  • Split string variable

    Hi!

    I have a database with unique id for reports related with companies information. I have a variable which contains the companies involved in the report. For example, for ID 1, I have in the column companies “Compay A”, for ID 2 I have this column “Company A; Company B, Company C”.

    I would like to split the variable companies and generate as many rows as companies involved in unique reports. So, I would like to have for the ID 2 three different rows, one for each, keeping the rest of variables equal. In other words, I would like to have a Panel Data, the ID is the company an the j variable the report (could be the same for some companies).

    thanks in advance.

  • #2
    Code:
    * Set up some sample data
    clear
    input id str40 name
    1 "Company A"
    2 "Company A; Company B, Company C"
    end
    
    * Actual answer starts here
    split name, p("," ";")
    
    drop name
    reshape long name, i(id) j(name_seq)
    drop if missing(name)
    list
    Please use dataex to post data in code form in future.

    This does not guarantee the name_seq will always be the same for each individual company. If you want a code to specifically represent a company, look into help encode.
    Last edited by Ken Chui; 23 Feb 2023, 08:48.

    Comment


    • #3
      Cross-posted and answered at https://stackoverflow.com/questions/...iable-in-stata

      Please note our policy on cross-posting, which is that you should tell us about it.https://www.statalist.org/forums/help#crossposting

      Comment

      Working...
      X