我在以下示例数据中创建一个名为result的变量,如果下注是赢,则显示'W',如果是下注,则显示'L'。
如何通过每个用户名的时间戳按行严格按行逐行连接此变量?
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)
答案 0 :(得分: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