Note that there are some explanatory texts on larger screens.

plurals
  1. POHandling string variables inside collapse command
    primarykey
    data
    text
    <p>Edit: I should have generated better data. It isn't necessarily the case that the string variable is <code>destring</code>able. I'm just being lazy here (I don't know how to generate random letters).</p> <hr> <p>I have a data set with a lot of strings that I want to <code>collapse</code>, but it seems that in general <code>collapse</code> doesn't place nicely with strings, particularly <code>(firstnm)</code> and <code>(count)</code>. Here are some similar data.</p> <pre><code>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()) } } </code></pre> <p>I want to <code>collapse</code> on "mark", which is simple enough with <code>replace</code> and subscripts.</p> <pre><code>replace mark = mark[_n - 1] if missing(mark) </code></pre> <p>But my <code>collapse</code>s fail with <code>type mismatch</code> errors.</p> <pre><code>collapse (firstnm) name (count) random, by(mark) </code></pre> <p>If I use <code>(first)</code>, then the first error clears, but <code>(count)</code> still fails. Is there a solution that avoids an additional <code>by</code> operation?</p> <p>It seems that the following works, but would also be a lot more time-consuming for my data.</p> <pre><code>generate nonmissing_random = !missing(random) egen nonmissing_random_count = count(nonmissing_random), by(mark) collapse (first) name nonmissing_random_count, by(mark) </code></pre> <p>Or is any solution that facilitates using <code>collapse</code> the same?</p>
    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.
 

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