Announcement

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

  • Split string and create one value per row

    Hi everybody

    I would like to split the variable Ovarighedimånedermax by id everytime ; appears so I only have one value per row

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str36 id byte Delkontrakter str917 Ovarighedimånedermax float group_index
    "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120"  1
    "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120"  2
    "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120"  3
    "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120"  4
    "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120"  5
    "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120"  6
    "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120"  7
    "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120"  8
    "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120"  9
    "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120" 10
    "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120" 11
    "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120" 12
    "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120" 13
    "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120" 14
    "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120" 15
    "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120" 16
    "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120" 17
    "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120" 18
    "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120" 19
    "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120" 20
    "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120" 21
    "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120" 22
    "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120" 23
    "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120" 24
    end
    Hopefully someone can help.

    Best
    Gustav

  • #2
    Try this. I am taking the liberty to drop duplicates of id and Ovarighedimånedermax, since all your observations seemed identical except for the group_index.

    Code:
    duplicates drop id Ovarighedimånedermax, force
    drop group_index
    
    split Ovarighedimånedermax, gen(wanted) parse(;)
    
    reshape long wanted, i(id Delkontrakter Ovarighedimånedermax) j(group_index)
    destring wanted, replace

    Comment


    • #3
      Thanks Hemanshu Kumar! Your code worked well; however, my data is a bit more complex. I have more ids with fewer contracts and additional variables like city that I need account for. So, how would you go about splitting the variable duration by id everytime ; appears so I only have one value per row (group_index).



      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input str36 id byte contracts str917 duration float(group_index City)
      "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120"  1 1
      "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120"  2 1
      "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120"  3 1
      "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120"  4 1
      "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120"  5 1
      "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120"  6 1
      "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120"  7 1
      "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120"  8 1
      "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120"  9 1
      "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120" 10 1
      "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120" 11 1
      "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120" 12 1
      "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120" 13 1
      "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120" 14 1
      "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120" 15 1
      "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120" 16 1
      "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120" 17 1
      "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120" 18 1
      "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120" 19 1
      "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120" 20 1
      "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120" 21 1
      "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120" 22 1
      "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120" 23 1
      "065351-2019" 24 "48;48;48;48;48;48;48;48;12;12;48;48;48;48;24;24;24;24;24;24;24;24;24;120" 24 1
      "111111-2019"  1 "36"                                                                        1 2
      "222222-2019"  2 "24;24"                                                                     1 3
      "222222-2019"  2 "24;24"                                                                     2 3
      end

      Comment


      • #4
        Gustav, your data example suggests that City, contracts and duration are all unique for an id. Moreover, contracts just appears to count the number of components of the duration string, and group_index just seems to be an attempt to index each component of duration. So the multiple observations for each id seem redundant.

        This may have resulted from a prior -reshape- of the data. It would probably be best to work with the data in the form it was in before that reshape.

        As it stands, my previous code pretty much still applies, with changes because of the new variable names and the additional variables included:

        Code:
        duplicates drop id, force
        drop group_index
        
        split duration, gen(wanted) parse(;)
        
        reshape long wanted, i(id contracts duration City) j(group_index)
        destring wanted, replace
        drop if missing(wanted)

        Comment


        • #5
          Thank you again Hemanshu Kumar! It works perfectly.

          Comment

          Working...
          X