Announcement

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

  • new variable concatenating names/labels of other variables by group

    Hello, I would like to create a variable that concatenate the names of other variables(even better the labels of the variable) by group.

    So with the example below I would like to get a new variable x equal to "osal" for id 58, to "osal osat" for id 59, "osam" for id 60, "osal osat osam" for id 21 and "osal osam" for id 22.

    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long id float(osal osat osam)
    58 1 0 0
    58 1 0 0
    58 1 0 0
    59 1 0 0
    59 1 1 0
    60 0 0 1
    21 0 1 1
    21 0 0 1
    21 1 0 1
    22 1 0 1
    22 0 0 0
    end
    ------------------ copy up to and including the previous line ------------------

    Thank you for any help!

  • #2
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long id float(osal osat osam)
    58 1 0 0
    58 1 0 0
    58 1 0 0
    59 1 0 0
    59 1 1 0
    60 0 0 1
    21 0 1 1
    21 0 0 1
    21 1 0 1
    22 1 0 1
    22 0 0 0
    end
    
    gen which = "" 
    
    quietly foreach v in osal osat osam { 
        bysort id (`v') : replace which = which + "`v' " if `v'[_N] 
    } 
    
    replace which = trim(which) 
    
    list, sepby(id) 
    
         +------------------------------------------+
         | id   osal   osat   osam            which |
         |------------------------------------------|
      1. | 21      0      0      1   osal osat osam |
      2. | 21      1      0      1   osal osat osam |
      3. | 21      0      1      1   osal osat osam |
         |------------------------------------------|
      4. | 22      0      0      0        osal osam |
      5. | 22      1      0      1        osal osam |
         |------------------------------------------|
      6. | 58      1      0      0             osal |
      7. | 58      1      0      0             osal |
      8. | 58      1      0      0             osal |
         |------------------------------------------|
      9. | 59      1      0      0        osal osat |
     10. | 59      1      1      0        osal osat |
         |------------------------------------------|
     11. | 60      0      0      1             osam |
         +------------------------------------------+

    Comment


    • #3
      Thank you Nick. What about labels? I get it with value labels but not with variable label.

      Comment


      • #4
        Put some variable labels in your data example and I will show you code. Alternatively, it is some variation on


        Code:
         
         bysort id (`v') : replace which = which + "`: var label `v'' " if `v'[_N]

        Comment


        • #5
          It works like it is!

          Comment

          Working...
          X