Announcement

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

  • Concatenation with missing values

    Dear all,

    I have been struggling for many days with this topic, I cannot seem to solve it. What I would like to do is create a variable that contains the content of many other variables. The problem lies in the fact that I have a lot of variables that have missing values. I do not want to include those in the concatenated variable that I end up with.
    I have, of course, found the concatenate function. However, this only partially solves the problem.

    egen CONCAT = concat(var1 var2 var3 var4), p( ; )



    Let's see if I can explain this by giving an example of what I would like to get in the end:
    500; 102; 565
    200; 354
    300; 569; 224; 5646

    What I end up now with is:
    500; 102; 565;
    200; 354;.;.
    300; 569; 224; 5646


    That bit with the "; . ;" is okay if there's only a few variables that I want to concatenate. My number of variables is 200+, however... So ;.;.;.;.;.;.;.;.;.;.;.;.;.;.;.;.;.;.;.;.;.;.;.;. contains no useful information and should ideally be removed.

    Thank you so much for your time!

    Kind regards,
    Erik


  • #2
    You can concatenate and then just edit out characters you don't want. Or you can just write a customised program. Let's do both.

    Code:
    clear 
    input var1 var2 var3 var4  
    500 102 565 . 
    200 354  . . 
    300 569 224 5646
    end 
    
    gen wanted1 = "" 
    
    forval j = 1/4 { 
        replace wanted1 = wanted1 + string(var`j') + ";" if var`j' < . 
    } 
    
    replace wanted1 = ///
    substr(wanted1, 1, length(wanted1) - 1) ///
    if substr(wanted1, length(wanted1), 1) == ";" 
    
    egen wanted2 = concat(var?), p(;) 
    replace wanted2 = subinstr(wanted2, ".;", "", .) 
    replace wanted2 = subinstr(wanted2, ";.", "", .) 
    
    list 
    
         +-----------------------------------------------------------------+
         | var1   var2   var3   var4            wanted1            wanted2 |
         |-----------------------------------------------------------------|
      1. |  500    102    565      .        500;102;565        500;102;565 |
      2. |  200    354      .      .            200;354            200;354 |
      3. |  300    569    224   5646   300;569;224;5646   300;569;224;5646 |
         +-----------------------------------------------------------------+
    Please read what the FAQ Advice says about photos of data. http://www.statalist.org/forums/help#stata

    Comment

    Working...
    X