[STATA] Unique Number of Observations

In research, we often have to figure out the number of unique observations-

For a small set of data, you can of course just use the command cookbook.

However, when you want to create a variable containing the unique number of observation by certain group then you can’t just use codebook.

Thankfully, we have the command egen to save the day-

**marks the unique observation

egen uniq=tag(file)

**Adds the unique observations and provides the total

egen tot_uniq=total(uniq), by(id)

I use this code to count the unique number of files for each firm or to count the number of unique directors for each firm-

It works well and it saves a ton of time-

Hope this code saves you some time and stress 😀

If this code solved a problem for you, please leave a comment-

Rejoice at all times. Pray without ceasing. Give thanks in every circumstance, for this is God’s will for you in Christ Jesus.

1 Thessalonians 5: 16-18

If you enjoy the content, you can support the blog through clicking the referral links that I use in my life: Acorns, Robinhood, American Express, dropbox, evernote, and Scribd.

Related Post

Leave a Reply

Your email address will not be published.