Announcement

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

  • Duplicate a row for a specific string variables

    Dear Stata Users,

    I am having the following dataset, which I want to create duplicates for different IDs separated by a comma:
    ID Director Name Gender Year of Birth
    US1535271068, US1535272058 David M 1980
    US2003401070 Alfred M 1970
    US57383M1080, US57383M2070, US57383T1034 Adam M 1960
    US2003401070 Helen F 1975
    I want to make it look like this:
    ID Director Name Gender Year of Birth
    US1535271068 David M 1980
    US1535272058 David M 1980
    US2003401070 Alfred M 1970
    US57383M1080 Adam M 1960
    US57383M2070 Adam M 1960
    US57383T1034 Adam M 1960
    US2003401070 Helen F 1975
    I am currently using this command, which not exactly what I want:
    Code:
     split ID, parse(,) generate(newID)
    Can you please help me with code?

    Many thanks,
    Panos
    Last edited by Panos Tzouvanas; 22 Aug 2023, 11:19.

  • #2
    You were on the right track. You just need to add some "finishing touches."

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str41 id str7 directorname str2 gender int yearofbirth
    "US1535271068, US1535272058 "               "David "  "M " 1980
    "US2003401070 "                             "Alfred " "M " 1970
    "US57383M1080, US57383M2070, US57383T1034 " "Adam "   "M " 1960
    "US2003401070 "                             "Helen "  "F " 1975
    end
    
    split id, gen(id) parse(",")
    drop id
    gen `c(obs_t)' obs_no = _n
    reshape long id, i(obs_no)
    drop obs_no _j
    drop if missing(id)
    order id, first
    
    list, noobs clean
    In the future, when showing data examples, please use the -dataex- command to do so, as I have here. If you are running version 18, 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment


    • #3
      Dear Clyde,

      Thanks for your help and guidance. It works fine!

      Regards,
      Panos

      Comment

      Working...
      X