Handling string variables inside collapse command

Edit: I had to generate better data. It is not necessary that the string variable destringis capable. I'm just lazy here (I don't know how to create random letters).


I have a data set with many rows that I want collapse, but it seems that in general it collapsedoes not fit nicely with rows, in particular (firstnm)and (count). Here are some similar data.

clear
set obs 9
generate mark = .
replace mark = 1 in 1
replace mark = 2 in 6
generate name = ""
generate random = ""
local i = 0
foreach first in Tom Dick Harry {
    foreach last in Smith Jones Jackson {
        local ++i
        replace name = "`first' `last'" in `i'
        replace random = string(runiform())
    }
}

I want collapseat the “mark”, which is quite simple with replaceand indexes.

replace mark = mark[_n - 1] if missing(mark)

But mine collapsefails with errors type mismatch.

collapse (firstnm) name (count) random, by(mark)

(first), , (count) . , by?

, , .

generate nonmissing_random = !missing(random)
egen  nonmissing_random_count = count(nonmissing_random), by(mark)
collapse (first) name nonmissing_random_count, by(mark)

, collapse ?

+4
2

destring random,replace, :

collapse (first) name (count) random, by(mark) 

mark    name    random
1   Tom Smith   5
2   Dick Jackson    4

collapse (firstnm) name (count) random, by(mark) .

+2

, egen count by . 1/0 / , (sum) .

generate nonmissing_random = !missing(random)
collapse (first) name (sum) nonmissing_random, by(mark)
+1

All Articles