2014-03-26 66 views
0

我在下面的示例數據中創建了一個名爲result的變量,如果下注是贏,則顯示'W',如果是虧損則顯示'L'。Stata:在條件下連接字符串變量

如何將每個用戶名的時間戳按嚴格順序逐行連接此變量與自身?

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

讀者應該注意,問題的最後一行是幻想語法;其餘的都會起作用。

這可能是你所尋求的。請注意,當您重新讀取數據時,您不能存在capture drop的變量。

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