Announcement

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

  • Reshaping with combinations of variable prefixes and suffixes/too many variables error

    Hello, I am trying to reshape data from wide to long with several variables.

    1) Is there a way I can reshape without listing all the variables bearing in mind that the prefixes and suffixes are in different combinations. I have seen a couple of examples on the forum; however, mainly the examples are for those with consistent labeling of variable suffixes and prefixes. Here is an example of the dataset.

    [CODE]
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(ALC8DDLY ALC8DTYP ALC8DWKY ALC8G7 ALC8_1 BPC8_10 BPC8_12 CCC8DANY CCC8GNUM CCC8_1A DGC8F1 DGC8G3A DGC8G3B DGC8_1A DGC8_1B DGC8_1C FH_8_38F FH_8_38G FIC8F1 FIC8_1 FIC8_2)
    0 2 0 .a 1 1 1 1 3 2 1 14 .a 1 2 2 2 2 2 2 2
    .a 3 .a .a 2 1 1 1 3 2 1 3 1 1 2 2 .a .a 2 2 2
    0 1 0 .a 1 1 1 1 3 2 1 14 1 2 2 2 .a .a 2 2 2
    end

    thanks in advance

  • #2
    I am probably not guessing right, as I see only consistency here. However, I have no idea what the data refer to.

    I can't comment on the error you report without seeing your code.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(ALC8DDLY ALC8DTYP ALC8DWKY ALC8G7 ALC8_1 BPC8_10 BPC8_12 CCC8DANY CCC8GNUM CCC8_1A DGC8F1 DGC8G3A DGC8G3B DGC8_1A DGC8_1B DGC8_1C FH_8_38F FH_8_38G FIC8F1 FIC8_1 FIC8_2)
    0 2 0 .a 1 1 1 1 3 2 1 14 .a 1 2 2 2 2 2 2 2
    .a 3 .a .a 2 1 1 1 3 2 1 3 1 1 2 2 .a .a 2 2 2
    0 1 0 .a 1 1 1 1 3 2 1 14 1 2 2 2 .a .a 2 2 2
    end
    
    foreach v of var * {
        local this = substr("`v'", 1, 4)
        local all `all' `this'
    }
    
    local all : list uniq all
    
    gen long id = _n
    reshape long `all', i(id) j(which) string
    
    l
    
         +------------------------------------------------------+
         | id   which   FH_8   ALC8   BPC8   CCC8   DGC8   FIC8 |
         |------------------------------------------------------|
      1. |  1    DANY      .      .      .      1      .      . |
      2. |  1    DDLY      .      0      .      .      .      . |
      3. |  1    DTYP      .      2      .      .      .      . |
      4. |  1    DWKY      .      0      .      .      .      . |
      5. |  1      F1      .      .      .      .      1      2 |
         |------------------------------------------------------|
      6. |  1     G3A      .      .      .      .     14      . |
      7. |  1     G3B      .      .      .      .     .a      . |
      8. |  1      G7      .     .a      .      .      .      . |
      9. |  1    GNUM      .      .      .      3      .      . |
     10. |  1      _1      .      1      .      .      .      2 |
         |------------------------------------------------------|
     11. |  1     _10      .      .      1      .      .      . |
     12. |  1     _12      .      .      1      .      .      . |
     13. |  1     _1A      .      .      .      2      1      . |
     14. |  1     _1B      .      .      .      .      2      . |
     15. |  1     _1C      .      .      .      .      2      . |
         |------------------------------------------------------|
     16. |  1      _2      .      .      .      .      .      2 |
     17. |  1    _38F      2      .      .      .      .      . |
     18. |  1    _38G      2      .      .      .      .      . |
     19. |  2    DANY      .      .      .      1      .      . |
     20. |  2    DDLY      .     .a      .      .      .      . |
         |------------------------------------------------------|
     21. |  2    DTYP      .      3      .      .      .      . |
     22. |  2    DWKY      .     .a      .      .      .      . |
     23. |  2      F1      .      .      .      .      1      2 |
     24. |  2     G3A      .      .      .      .      3      . |
     25. |  2     G3B      .      .      .      .      1      . |
         |------------------------------------------------------|
     26. |  2      G7      .     .a      .      .      .      . |
     27. |  2    GNUM      .      .      .      3      .      . |
     28. |  2      _1      .      2      .      .      .      2 |
     29. |  2     _10      .      .      1      .      .      . |
     30. |  2     _12      .      .      1      .      .      . |
         |------------------------------------------------------|
     31. |  2     _1A      .      .      .      2      1      . |
     32. |  2     _1B      .      .      .      .      2      . |
     33. |  2     _1C      .      .      .      .      2      . |
     34. |  2      _2      .      .      .      .      .      2 |
     35. |  2    _38F     .a      .      .      .      .      . |
         |------------------------------------------------------|
     36. |  2    _38G     .a      .      .      .      .      . |
     37. |  3    DANY      .      .      .      1      .      . |
     38. |  3    DDLY      .      0      .      .      .      . |
     39. |  3    DTYP      .      1      .      .      .      . |
     40. |  3    DWKY      .      0      .      .      .      . |
         |------------------------------------------------------|
     41. |  3      F1      .      .      .      .      1      2 |
     42. |  3     G3A      .      .      .      .     14      . |
     43. |  3     G3B      .      .      .      .      1      . |
     44. |  3      G7      .     .a      .      .      .      . |
     45. |  3    GNUM      .      .      .      3      .      . |
         |------------------------------------------------------|
     46. |  3      _1      .      1      .      .      .      2 |
     47. |  3     _10      .      .      1      .      .      . |
     48. |  3     _12      .      .      1      .      .      . |
     49. |  3     _1A      .      .      .      2      2      . |
     50. |  3     _1B      .      .      .      .      2      . |
         |------------------------------------------------------|
     51. |  3     _1C      .      .      .      .      2      . |
     52. |  3      _2      .      .      .      .      .      2 |
     53. |  3    _38F     .a      .      .      .      .      . |
     54. |  3    _38G     .a      .      .      .      .      . |
         +------------------------------------------------------+

    Comment


    • #3
      Thank you so much Nick it worked

      Comment

      Working...
      X