【问题标题】:Stata: Concatenate string variable on by conditionStata:按条件连接字符串变量
【发布时间】:2014-03-26 15:13:28
【问题描述】:

我在以下示例数据中创建了一个名为 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) 

【问题讨论】:

    标签: stata


    【解决方案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 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多