Announcement

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

  • Extract different parts from one variable

    Dear experts,

    I have a table like this:

    Year Violation Year

    2010 2005,2006,2007,2008,2009,2010

    Both "Year"and "Violation Year" are string. How can I extract the multiple years in "Violation Year" seperately to make the below happen?

    Year Violation Year

    2010 2005
    2010 2006
    2010 2007
    2010 2008
    2010 2009
    2010 2010

    Thank you so much!

  • #2
    Dear Experts,

    I am new to Stata, I have survey results on the Stata. In the survey different question were asked from the respondents. is there any possibility to group these respondents by for example the type of work they do and how ?. However, there is no variable to indicate this.

    thanks

    Comment


    • #3
      Abdurashid Bozorov Welcome to Statalist.

      You apparently created your post by clicking on "Post Reply" while you were reading the first post in this topic. Since it is a separate question you should return to the General Forum page that lists the most recent topics and click "+ New Topic" on that page and create a new topic with an appropriate title and your question.

      Ke Gong

      Here is some sample code that should start you in a useful direction.
      Code:
      * Input the example data
      
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input int year str29 viol
      2010 "2005,2006,2007,2008,2009,2010"
      end
      
      * Convert the example data
      
      split viol, parse(,) destring
      list, clean noobs
      drop viol
      reshape long viol, i(year) j(violnum)
      list, clean noobs
      Code:
      . split viol, parse(,) destring
      variables born as string: 
      viol1  viol2  viol3  viol4  viol5  viol6
      viol1: all characters numeric; replaced as int
      viol2: all characters numeric; replaced as int
      viol3: all characters numeric; replaced as int
      viol4: all characters numeric; replaced as int
      viol5: all characters numeric; replaced as int
      viol6: all characters numeric; replaced as int
      
      . list, clean noobs
      
          year                            viol   viol1   viol2   viol3   viol4   viol5   viol6  
          2010   2005,2006,2007,2008,2009,2010    2005    2006    2007    2008    2009    2010  
      
      . drop viol
      
      . reshape long viol, i(year) j(violnum)
      (note: j = 1 2 3 4 5 6)
      
      Data                               wide   ->   long
      -----------------------------------------------------------------------------
      Number of obs.                        1   ->       6
      Number of variables                   7   ->       3
      j variable (6 values)                     ->   violnum
      xij variables:
                        viol1 viol2 ... viol6   ->   viol
      -----------------------------------------------------------------------------
      
      . list, clean noobs
      
          year   violnum   viol  
          2010         1   2005  
          2010         2   2006  
          2010         3   2007  
          2010         4   2008  
          2010         5   2009  
          2010         6   2010

      Comment


      • #4
        @William Lisowski

        This is awesome! It works perfectly!! I do appreciate your prompt help!! Have a wonderful weekend!

        Comment

        Working...
        X