【问题标题】:How to make postfile work with both string and numeric variable如何使 postfile 同时使用字符串和数字变量
【发布时间】:2019-03-13 08:38:43
【问题描述】:

我正在尝试编写一个通过标识符进行一些计算的 Stata 程序,我希望它可以使标识符可以是字符串或整数。

我正在尝试做的一个非常简化的版本是这样的:

clear all

***** test data
input str10 id1 id2 x y
a   1   20  40
a   1   140 20
a   1   0   70
b   2   50  25
b   2   25  50
b   2   40  42
end

*****
capture program drop myprog
program define myprog
    version 14.2
    syntax using, ID(varname) Mean(varname)
    tempname postname

    quietly levelsof `id', local(ids)
    local idtype: type `id'

    postfile `postname' `idtype' `id' `mean' `using', replace


    foreach i of local ids {
        quietly summarize `mean' if `id'==`i'
        post `postname' (`i') (`r(mean)')
    }

    postclose `postname'
end

并且我希望以下两个都可以工作:

myprog using "test1.dta", id(id1) mean(x)
myprog using "test2.dta", id(id2) mean(x)

有什么建议吗?

【问题讨论】:

    标签: stata postfile


    【解决方案1】:

    只需使用if / else 语句来区分这两种情况:

    capture program drop myprog
    program define myprog
        version 14.2
        syntax using, ID(varname) Mean(varname)
        tempname postname
    
        quietly levelsof `id', local(ids)
        local idtype: type `id'
    
        postfile `postname' `idtype' `id' `mean' `using', replace
    
        if substr("`idtype'" , 1, 3) == "str" {
            foreach i of local ids {
                summarize `mean' if `id'=="`i'", meanonly 
                post `postname' ("`i'") (`r(mean)')
            }
        } 
        else {
            foreach i of local ids { 
                summarize `mean' if `id'==`i', meanonly 
                post `postname' (`i') (`r(mean)')       
            }
        }
    
        postclose `postname'
    end
    

    顺便提一下,注意summarizemeanonly 选项的使用。

    【讨论】:

    • 谢谢!事实证明,我距离我的答案只有 10 分钟的谷歌搜索,最终我做了与你所做的类似的事情。
    【解决方案2】:

    这就是我最终做的:

    capture program drop myprog
    program define myprog
        version 14.2
        syntax using, ID(varname) Mean(varname)
        tempname postname
    
        quietly levelsof `id', local(ids)
        local idtype: type `id'
        postfile `postname' `idtype' `id' `mean' `using', replace
    
        capture confirm string variable `id'
    
        if !_rc {
            foreach i of local ids {
                quietly summarize `mean' if `id'=="`i'"
                post `postname' ("`i'") (`r(mean)')
            }
        }
        else {
            foreach i of local ids {
                quietly summarize `mean' if `id'==`i'
                post `postname' (`i') (`r(mean)')
            }
        }
    
        postclose `postname'
    end
    

    这两个几乎相同的循环看起来很难看,但我想这很好。

    【讨论】:

    • 查看@Pearly Spencer 答案的修订版,它与您的答案更接近。您也可以使用egen newid = group(id), label,然后发布newid 的值标签,它们始终是字符串。这样一来,就只有一个循环。
    • 好建议。不幸的是,在我的实际用例中,我会将 postfile 结果合并回另一个“主”数据集,我必须保留数据类型。
    猜你喜欢
    • 2018-12-30
    • 1970-01-01
    • 2018-04-16
    • 2016-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-31
    相关资源
    最近更新 更多