Question

How can I get a count of the total number of observations for each unique instances of resulthistory in the following MWE?

clear
input str16 username betnumber stake probability subsequentwinnings
player1 1 10 0.5 -10
player2 1 10 0.5  10
player3 1 10 0.5 -10
player4 1 10 0.5 -10

player2 2 20 0.5 -20
player3 2 20 0.5 -20
end

generate result = "L"
replace result = "W" if (subsequentwinnings >0)

bysort username (betnumber): gen resulthistory = result[1] 
by username : replace resulthistory = resulthistory[_n-1] + result if _n > 1 

Do I need to issue preserve/restore commands, aggregating by resulthistory between them?

Was it helpful?

Solution

Let's answer your question as you pose it. Unique occurrences occur once only; that's what the word means. So, each unique value occurs once, and you don't need a program to calculate 1 as an answer. You might want to know which results histories occur just once, and those are identifiable by an indicator variable

bysort resultshistory : gen unique = _N == 1 

Now let's suppose that you didn't mean what you said. I advised in an earlier answer reading

On distinct observations (and why the word "unique" is misleading): http://www.stata-journal.com/sjpdf.html?articlenum=dm0042

As that's a review of the entire question, with details on various commands to be used, I'll stop there.

Your various questions are good at showing what code you have before you have a question, but you don't always show code that is an attempt to answer your question. Nor do you show explicitly that you are looking at documentation, including StataCorp FAQs and accessible material in the Stata Journal, so although I am trying to help you, I am also going to downvote you as not showing attempted code solutions and not showing research effort.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top