0
votes

I'm creating a variable called result in the following sample data which shows a 'W' if a bet was a win and 'L' if it was a loss.

How can I concatenate this variable with itself on a row by row basis in strict order by timestamp for each username?

clear
input str16 username str40 betdate winnings
player1 "12NOV2008:19:04:01" -10
player1 "12NOV2008:12:03:44" 50
player2 "07NOV2008:14:03:33" -50
player2 "05NOV2008:09:00:00" -100
end

generate double timestamp=clock(betdate,"DMY hms") 
format timestamp %tc

cap drop result
generate result = "L"
replace result = "W" if (winnings >0)

cap drop resulthistory
generate resulthistory = ""
replace resulthistory = concat(resulthisory + result), by(USERNAME timestamp) 
1

1 Answers

1
votes

Readers should note that the last line of the question is fantasy syntax; the rest would work.

This may be what you seek. Note that as you read in the data afresh, the variables you capture drop can't exist.

clear
input str16 username str40 betdate winnings
player1 "12NOV2008:19:04:01" -10
player1 "12NOV2008:12:03:44" 50
player2 "07NOV2008:14:03:33" -50
player2 "05NOV2008:09:00:00" -100
end

gen double timestamp=clock(betdate,"DMY hms") 
format timestamp %tc

gen result = cond(winnings > 0, "W", "L") 

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

list