Announcement

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

  • Creating observation ( based on "/" from another obs)

    Code:
      +-------------------------------------------------------------------+
      |                       school                               campus |
      |-------------------------------------------------------------------|
      |          School A / School B                  Location1/Location2 |
      |                     School C                           Location 3 |
      | School X/ School Y /School Z   Location 4/ Location 5 /Location 6 |
      +-------------------------------------------------------------------+
    I would like to create new observations by taking "/" into account. For example, obs1 will be school==School A and campus==Location1 ; obs2 will be school==School B and campus==Location2.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str28 school str34 campus
    "School A / School B"          "Location1/Location2"               
    "School C"                     "Location 3"                        
    "School X/ School Y /School Z" "Location 4/ Location 5 /Location 6"
    end

  • #2
    Maybe like this:

    Code:
    . split school,  parse(/)
    variables created as string: 
    school1  school2  school3
    
    . split  campus,  parse(/)
    variables created as string: 
    campus1  campus2  campus3
    
    . gen id = _n
    
    . drop school campus
    
    . reshape long school campus, i(id)
    (note: j = 1 2 3)
    
    Data                               wide   ->   long
    -----------------------------------------------------------------------------
    Number of obs.                        3   ->       9
    Number of variables                   7   ->       4
    j variable (3 values)                     ->   _j
    xij variables:
                    school1 school2 school3   ->   school
                    campus1 campus2 campus3   ->   campus
    -----------------------------------------------------------------------------
    
    . list , clean
    
           id   _j       school         campus  
      1.    1    1    School A       Location1  
      2.    1    2     School B      Location2  
      3.    1    3                              
      4.    2    1     School C     Location 3  
      5.    2    2                              
      6.    2    3                              
      7.    3    1     School X     Location 4  
      8.    3    2    School Y     Location 5   
      9.    3    3     School Z     Location 6  
    
    . drop if missing(school) | missing(campus)
    (3 observations deleted)
    
    
    
    . list, clean
    
           id   _j       school         campus  
      1.    1    1    School A       Location1  
      2.    1    2     School B      Location2  
      3.    2    1     School C     Location 3  
      4.    3    1     School X     Location 4  
      5.    3    2    School Y     Location 5   
      6.    3    3     School Z     Location 6  
    
    .

    Comment


    • #3
      Many thanks Joro Kolev! Your code does the job.

      Comment

      Working...
      X