Announcement

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

  • concat many string variables

    I have string variables e.g assets1-assets30 and i wanted to concat and seperate them by comma.
    I have used this : egen x= concat(assets*), punct(",") but the resulting results has even commas from the variables with missing data.
    Is there a way to return a concatenated variables only for the places where the variable is not missing.
    Here is the sample data:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str40(assets15_20 assets16_21 assets17_22 assets18_23)
    "Bicycle"                 "Solar Panel"                          "Radio,Tape or CD player"              "Television"  
    "Radio,Tape or CD player" "Television"                           "Other Agricultural machines or tools" ""            
    "Mosquito net"            "Other Agricultural machines or tools" ""                                     ""            
    "Bicycle"                 "Radio,Tape or CD player"              "Television"                           "Mosquito net"
    ""                        ""                                     ""                                     ""            
    end

  • #2
    You can do it but I don't see that you will get anything useful unless typical resulting strings are all short.

    Code:
    gen all = assets1 
    
    forval j = 2/30 { 
        replace all = all + "," + assets`j' if trim(assets`j') != "" 
    }

    Comment


    • #3
      Thank you so much Nick. Very helpful and i appreciate.

      Comment

      Working...
      X