美文网首页stata数据清理与高效语法
stata--分组变量生成利器recode命令

stata--分组变量生成利器recode命令

作者: 古城路揸fit人 | 来源:发表于2019-10-13 21:01 被阅读0次

    用途

    比如有教育类型要生成教育年限。

    基本语法

    recode varlist (rule) [(rule) ...] [, generate(newvar)]
    // default is to replace existing variables。不加gen就是直接替换
    

    举例

    webuse recxmpl
    recode x2 (1 2 = 1) (3 = 2) (4/7 = 3), gen(nx2) //新变量1和2变为1;3变为2;4到7变为3
    list x2 nx2
    

    直接和label结合

    webuse fullauto, clear
    recode rep77 rep78 (1 2 = 1 "Below average") ///
    (3 = 2 Average) (4 5 = 3 "Above average"), pre(new) label(newrep) //加入前缀
    

    和最大最小结合

    sysuse cancer, clear
    la var died "Patient died"
    la def ny 0 "No" 1 "Yes", modify
    la val died ny
    recode studytime ///
    (min/10 = 1 "10 or less months") ///
    (11/20 = 2 "11 to 20 months") ///
    (21/30 = 3 "21 to 30 months") ///
    (31/max = 4 "31 or more months") ///
    , gen(stime)
    
    示意图

    相关文章

      网友评论

        本文标题:stata--分组变量生成利器recode命令

        本文链接:https://www.haomeiwen.com/subject/qdykmctx.html