Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I rearranged my approach to be more clear. You mentioned that you can't recode your variables, but I'm not sure if there's a way around that (I think any solution here recodes either explicitly or implicitly). Of course, you'll need to replace "4" with "20" throughout.</p> <pre><code>* generate some projects and members clear set obs 5 generate int project = _n generate person_1 = "Tom" generate person_2 = "Dick" if (_n &gt;= 3) generate person_3 = "Harry" if (_n &gt;=5) replace person_1 = "Jane" if inlist(_n, 2, 4) tempfile orig save `orig' * reshape to long reshape long person_, i(project) string drop _j drop if missing(person) sort project person egen id = group(person) drop if missing(id) reshape wide person, i(project) j(id) * recode to allow easier group identification forvalues i = 1/4 { levelsof person_`i', local(name) clean generate byte d_person_`i' = cond(missing(person_`i'), 0, 1) label define d_person_`i'_lbl 1 "`name'" 0 "" label values d_person_`i' d_person_`i'_lbl } * determine number of workers on project egen gp_size = rowtotal(d_person_*) * unique id for each group composition generate int id = 0 forvalues i = 1/4 { local two_i = 2^(`i' - 1) replace id = id + d_person_`i' * `two_i' } * group members generate str mbrs = "" forvalues i = 1/4 { local name: label d_person_`i'_lbl 1 replace mbrs = mbrs + "/" + "`name'" if (d_person_`i' == 1) } * there's always a leading "/" to remove with this approach replace m = substr(m, 2, .) * merge back your orig data merge 1:1 project using `orig', nogenerate replace update </code></pre> <p>This yields:</p> <pre><code>. list +---------------------------------------------------------------------------------------------------------------------------------+ | project person_1 person_2 person_3 person_4 d_pers~1 d_pers~2 d_pers~3 d_pers~4 gp_size id mbrs | |---------------------------------------------------------------------------------------------------------------------------------| 1. | 1 Tom Tom Tom 1 8 Tom | 2. | 2 Jane Jane Jane 1 4 Jane | 3. | 3 Tom Dick Tom Dick Tom 2 9 Dick/Tom | 4. | 4 Jane Dick Jane Dick Jane 2 5 Dick/Jane | 5. | 5 Tom Dick Harry Tom Dick Harry Tom 3 11 Dick/Harry/Tom | +---------------------------------------------------------------------------------------------------------------------------------+ </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload