使用encode以我想要的顺序使用字符串标签创建数字

时间:2016-08-05 15:10:28

标签: stata

我已经阅读了如何做到这一点,我想我知道,基于这篇文章: http://www.stata.com/support/faqs/data-management/ordering-results-of-tabulation/

DEBTPhaseString是一个带有六个值的字符串变量:Start,Readiness,On The Path,Nearing the Finish,Out of Poverty和Completed,按顺序。

我正在尝试对此进行编码,以便变量按顺序tabulate,但变量仍然按字母顺序列表。这是我的代码。

label define PhaseOrder 1 "Start" 2 "Readiness" 3 "On The Path" 4 "Nearing The Finish" 5 "Out of Poverty" 6 "Completed"

gen DEBTPhaseString = "On The Path"
replace DEBTPhaseString = "Nearing The Finish" if DEBTNegativeMR<=OTPDebtGoal
replace DEBTPhaseString = "Out of Poverty" if DEBTNegativeMR<=NTFDebtGoal
replace DEBTPhaseString = "Completed" if DEBTNegativeMR==0
replace DEBTPhaseString = "" if DEBTNegativeMR==.
encode DEBTPhaseString, gen(DEBTPhase) label(PhaseOrder)
tab DEBTPhase

tab result

1 个答案:

答案 0 :(得分:1)

好消息是你的策略是完全正确的。坏消息是你的问题不可重复。我们没有您的数据集,也无法查看您可能相关的所有代码。另请参见一般https://stackoverflow.com/help/mcve

这个例子是独立的,原则证明。

在某处会有一些小错字 - 变量名称之间的混淆或者其他一些 - 但我们,更确切地说,我不能说出它是什么。仔细检查例如describe DEBT*的结果。

clear 
set obs 6 

gen DEBTPhaseString = "On The Path" in 1 
replace DEBTPhaseString = "Nearing The Finish" in 2
replace DEBTPhaseString = "Out of Poverty" in 3 
replace DEBTPhaseString = "Completed" in 4 
replace DEBTPhaseString = "Start" in 5 
replace DEBTPhaseString = "Readiness" in 6  

label define PhaseOrder 1 "Start" 2 "Readiness" 3 "On The Path" 4 "Nearing The Finish" 5 "Out of Poverty" 6 "Completed"
encode DEBTPhaseString, gen(DEBTPhase) label(PhaseOrder)

tab DEBTPhase

         DEBTPhase |      Freq.     Percent        Cum.
-------------------+-----------------------------------
             Start |          1       16.67       16.67
         Readiness |          1       16.67       33.33
       On The Path |          1       16.67       50.00
Nearing The Finish |          1       16.67       66.67
    Out of Poverty |          1       16.67       83.33
         Completed |          1       16.67      100.00
-------------------+-----------------------------------
             Total |          6      100.00