0
votes

Currently I have a dataset that appears as follows:

 mnbr      firm       contribution
 1591      2          1
 9246      6          1
 812       6          1
 674       6          1

And so on. The idea is that mnbr is the member number of employees who work at firm # whatever. If contribution is 1 (and I have dropped all the 0s for this purpose) said employee has contributed to a certain fund.
I additionally used codebook to determine the number of unique firms that exist. The goal is to determine the average number of contributions per firm i.e. there was 1 contribution for firm 2, 3 contributions for firm 6 and so on. The problem I arrive at is accessing that the unique values number from codebook.
I read some documentation online for

inspect *varlist*
display r(N_unique)

which suggests to me that using r(N_unique) would store that value, yet unfortunately this method did not work for me. So that is part 1.

Part 2 is I'd also like to create a variable that shows the contributions in each firm i.e.

 mnbr      firm       contribution      average
 1591      2          1                 1
 9246      6          .                 2/3
 812       6          1                 2/3
 674       6          1                 2/3

to show that for firm 6, 2 out of the 3 employees contributed to this fund.
Thanks in advance for the help.

1
@RobertoFerrer I already tried that documentation. Upon implementing the code I received the error "r(101) factor variables and time-series operators not allowed." Any ideas?thyde
"Did not work for me" is not a good way to report problems. People are left guessing what went wrong. It's also good idea to post exact code and errors, if any. The stored result r(N_unique) will be that of the last variable in the variable list. Try inspect firm (only one variable; the one you are interested, I believe).Roberto Ferrer
@RobertoFerrer When I implement inspect firm I get the desired output that produces a histogram and a table with the number of positive, zero, and negative entries among others. However when I try to implement display r(N_unique) the output is only . and stata does not appear to be working on it any further. Hope that makes sense, it just outputs nothing for me, which is why I said it did not work for me, though I certainly should have clarified).thyde
See stata-journal.com/sjpdf.html?articlenum=dm0042 for a review of calculating the number of distinct observations, including why "unique" is poor terminology for this problem.Nick Cox

1 Answers

1
votes

To answer your comment, this works for me:

clear
set more off 

input ///
 mnbr      firm       cont
 1591      2          1
 9246      6          .
 812       6          1
 674       6          1
 end

list

// problem 1
inspect firm
display r(N_unique)

// problem 2
bysort firm: egen totc = total(cont)
by firm: gen share = totc / _N

list

You have to use r(N_unique) before running another Stata command, or it can get lost. You can also save that result to a local or scalar.

Problem 2 is also addressed.