Announcement

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

  • Xpose vs reshape

    Hi all, from a previous question, I ended up with data after -xpose- that looks like the below:


    \
    Code:
    sysuse nlsw88, clear
    
    #delimit ;
    collapse 
        (percent) perc = idcode 
        (mean) m_age = age m_married = married m_south = south m_smsa = smsa 
        (semean) se_age = age se_married = married se_south = south se_smsa = smsa 
        (count) idcode 
            if !missing(union), by(union)
        ;
    #delimit cr
    
    replace perc = perc/100
    
    
    list
    
    
         +---------------------------------------------------------------------------------------------------------+
      1. |    union |    perc |   m_age | m_marr~d | m_south |   m_smsa |  se_age | se_mar~d | se_south |  se_smsa |
         | nonunion | .754526 | 39.2054 |  .664785 |  .46789 | .6873677 | .080744 |  .012545 |   .01326 | .0123191 |
         |---------------------------------------------------------------------------------------------------------|
         |                                                 idcode                                                  |
         |                                                   1417                                                  |
         +---------------------------------------------------------------------------------------------------------+
    
         +---------------------------------------------------------------------------------------------------------+
      2. |    union |    perc |   m_age | m_marr~d | m_south |   m_smsa |  se_age | se_mar~d | se_south |  se_smsa |
         |    union | .245474 | 39.2842 |  .607375 | .295011 | .7700651 | .140763 |  .022769 |  .021263 | .0196195 |
         |---------------------------------------------------------------------------------------------------------|
         |                                                 idcode                                                  |
         |                                                    461                                                  |
         +---------------------------------------------------------------------------------------------------------+
    
    
    
    xpose, clear varname
    
    drop in 1
    
    rename (v1 v2) (union nonunion)
    
    
    list
    
         +----------------------------------+
         |    union   nonunion     _varname |
         |----------------------------------|
      1. | .7545261   .2454739         perc |
      2. | 39.20536   39.28416        m_age |
      3. | .6647847   .6073753    m_married |
      4. | .4678899   .2950108      m_south |
      5. | .6873677   .7700651       m_smsa |
         |----------------------------------|
      6. | .0807436   .1407625       se_age |
      7. |  .012545   .0227687   se_married |
      8. | .0132599   .0212633     se_south |
      9. | .0123191   .0196195      se_smsa |
     10. |     1417        461       idcode |
         +----------------------------------+
    However, I would ideally want to have a union mean column, union SE column, nonunion mean column, and nonunion SE column. In looking at the xpose help file, I don't see an option for splitting based on a variable root--so I thought of trying to do a reshape but faltered because I have the "union" and "perc" variables on their own alongside the m_ and se_ sequences. Any ideas on the best way to go about this?

  • #2
    You can do this with a -reshape long- followed by a -reshape wide-, changing the -i()- and -j()- options:
    Code:
    rename perc m_perc
    reshape long m_ se_, i(union) j(vble) string
    decode union, gen(_union)
    drop union idcode
    reshape wide m_ se_, i(vble) j(_union) string

    Comment


    • #3
      A double reshape is not something I had tried before. Thanks.

      Comment

      Working...
      X