Stata 如何使postfile同时使用字符串和数字变量

Stata 如何使postfile同时使用字符串和数字变量,stata,postfile,Stata,Postfile,我正在尝试编写一个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 m

我正在尝试编写一个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)

有什么建议吗?

只要使用
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

顺便说一句,注意使用了
summary
meanonly
选项

只需使用
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

顺便说一句,注意使用了
summary
meanonly
选项

这就是我最后要做的:

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

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

这就是我最后做的:

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

这两个几乎相同的环看起来很难看,但我想没关系。

谢谢!事实证明,我只需再花10分钟在谷歌上搜索我的答案,结果我做了与你类似的事情。谢谢!事实证明,我只需在谷歌上搜索10分钟就可以得到答案,结果我做了与你类似的事情。请看@Pearly Spencer答案的修订版,它更接近你的答案。您还可以使用
egen newid=group(id),label
,然后发布
newid
的值标签,这些标签总是字符串。这样,就只有一个循环了。好建议。不幸的是,在我的实际用例中,我将把postfile结果合并回另一个“主”数据集,并且我必须保留数据类型。请参阅@Pearly Spencer答案的修订版,它更接近您的答案。您还可以使用
egen newid=group(id),label
,然后发布
newid
的值标签,这些标签总是字符串。这样,就只有一个循环了。好建议。不幸的是,在我的实际用例中,我将把postfile结果合并回另一个“主”数据集,并且我必须保留数据类型。