从多个变量中提取字符串的循环

时间:2018-06-19 12:46:50

标签: loops stata

我正在尝试自动化为上一篇文章中描述的问题提供的解决方案:

我无法使每个变量CTSNo的以下循环正常工作:

foreach i in CTSNo1-CTSNo54{

generate tag = 0
replace tag = 1 if strmatch(CTSNo1, "*-*")

keep if tag == 1
generate part1 = regexs(0) if regexm(CTSNo1, "([0-9]+)")
generate part2 = substr(regexs(0), 2, .) if regexm(CTSNo1, "-.*([0-9])")

local obs = _N

forvalues i = 1 / `obs' {
   local xpa = abs(real(part1[`i']) - real(part2[`i'])) + 1
   expand `xpa' if _n == `i'        
}

bysort SRNo (CTSNo1): egen interim = seq()
bysort SRNo (CTSNo1): generate NCTSNo1 = real(part1) + interim - 1

drop tag part1 part2 interim

order SRNo SchemeName SchemeAddress ProposalNo CTSNo1 NCTSNo1 CTSNo2  

}  

我该怎么做?


编辑:

示例数据:

input str30 CTSNo1 float NCTSNo1 str4 CTSNo2 str24 CTSNo3 str20 CTSNo4 
str13 CTSNo5 str7 CTSNo6 str17 CTSNo7 str11 CTSNo8 str18 CTSNo9 str3 
CTSNo10
"455-D"        455 "" " 455-D/2 - 24"  ""               "" "" ""             
"" ""                ""
"156-B"        156 "" " 156-B/36 - 57" " 156-B/70 - 89" "" "" " 156-D 
(pt.)" "" " 156-D/158 - 69" ""
"938-A (Part)" 938 "" ""               ""               "" "" ""             
"" ""                ""
"631 Part - 3" 631 "" ""               ""               "" "" ""             
"" ""                ""
"631 Part - 3" 632 "" ""               ""               "" "" ""             
"" ""                ""
"631 Part - 3" 633 "" ""               ""               "" "" ""             
"" ""                ""
"631 Part - 3" 634 "" ""               ""               "" "" ""             
"" ""                ""
"631 Part - 3" 635 "" ""               ""               "" "" ""             
"" ""                ""
"631 Part - 3" 636 "" ""               ""               "" "" ""             
"" ""                ""
"631 Part - 3" 637 "" ""               ""               "" "" ""             
"" ""                ""
"631 Part - 3" 638 "" ""               ""               "" "" ""             
"" ""                ""

我希望每个CTSNo后面都有一个NCTSNo,以扩展值。例如,对于156-B / 36、156-B / 37,156-B / 36-57应该有单独的行,一直到156-B / 57。

(我也不确定为什么要扩展631 Part-3,并希望像455-D这样的值保留-D而不是仅仅保留455。)

1 个答案:

答案 0 :(得分:1)

根据示例数据,您无法概括以前文章中的代码。这是因为CTSNo变量的内容没有呈现出一致的模式。因此,他们需要应用不同的规则,以便从每个规则中获取所需信息。

但是,在下面,您可以找到一个简化的玩具示例,该示例演示了当有多个CTSNo变量( 全部 156-B/36-57形式的strong>。

示例数据:

clear

input int SRNo str200 SchemeName str30 CTSNo3 str15 CTSNo4 str15 CTSNo9
69 "SRA Co-op.Housing Society Ltd." " 156-F/233 - 47" ""
70 "Jai Bhavani CHS Ltd. (Proposed)" "7 (Pt.)" ""
71 "Bhavani Housing" " "  "156-B/70 - 89 " ""
72 "Shivshakti SRA CHS Ltd." "364 ‘A’" ""
73 "Shree Ram CHS Ltd. (Prop.)" " " ""
74 "Ram CHS Ltd. (Prop.)" " " " " " 156-D/158 - 69 "
end

list

     +---------------------------------------------------------------------------------------------+
     | SRNo                        SchemeName            CTSNo3           CTSNo4            CTSNo9 |
     |---------------------------------------------------------------------------------------------|
  1. |   69    SRA Co-op.Housing Society Ltd.    156-F/233 - 47                                    |
  2. |   70   Jai Bhavani CHS Ltd. (Proposed)           7 (Pt.)                                    |
  3. |   71                   Bhavani Housing                     156-B/70 - 89                    |
  4. |   72           Shivshakti SRA CHS Ltd.           364 ‘A’                                    |
  5. |   73        Shree Ram CHS Ltd. (Prop.)                                                      |
     |---------------------------------------------------------------------------------------------|
  6. |   74              Ram CHS Ltd. (Prop.)                                       156-D/158 - 69 |
     +---------------------------------------------------------------------------------------------+

修改后的代码段:

preserve

generate tag = 0
replace tag = 1 if !strmatch(CTSNo3, "*/*") & !strmatch(CTSNo4, "*/*") & ///
                   !strmatch(CTSNo9, "*/*")
keep if tag == 1
save base, replace

restore

ds CTSNo*
local CTSvars "`r(varlist)'"

foreach var of local CTSvars {
    preserve

    generate tag = 0
    replace tag = 1 if strmatch(`var', "*/*")

    keep if tag == 1

    generate part0 = substr(`var', 1, strpos(`var', "/"))
    generate part1 = substr(`var', strpos(`var', "/") + 1, strpos(`var', "-") - 1)
    generate part2 = substr(`var', strrpos(`var', "-") + 1, .)

    if strlen(trim(part1)) == 3 {
        generate xpa = abs(real(substr(part1, 2, .)) - real(part2)) + 1 
    }
    else {
        generate xpa = abs(real(part1) - real(part2)) + 1
    }

    expand xpa

    bysort SRNo (`var'): egen interim = seq()
    bysort SRNo (`var'): generate N`var' = part0 + string(real(part1) + interim - 1)

    drop tag part0 part1 part2 xpa interim

    list
    save `var', replace
    restore
}

use base, clear
append using `CTSvars'

sort SRNo NCTS*

最终结果:

list SRNo SchemeName NCTS*
     +-----------------------------------------------------------------------------+
     | SRNo                        SchemeName      NCTSNo3    NCTSNo4      NCTSNo9 |
     |-----------------------------------------------------------------------------|
  1. |   69    SRA Co-op.Housing Society Ltd.    156-F/233                         |
  2. |   69    SRA Co-op.Housing Society Ltd.    156-F/234                         |
  3. |   69    SRA Co-op.Housing Society Ltd.    156-F/235                         |
  4. |   69    SRA Co-op.Housing Society Ltd.    156-F/236                         |
  5. |   69    SRA Co-op.Housing Society Ltd.    156-F/237                         |
     |-----------------------------------------------------------------------------|
  6. |   69    SRA Co-op.Housing Society Ltd.    156-F/238                         |
  7. |   69    SRA Co-op.Housing Society Ltd.    156-F/239                         |
  8. |   69    SRA Co-op.Housing Society Ltd.    156-F/240                         |
  9. |   69    SRA Co-op.Housing Society Ltd.    156-F/241                         |
 10. |   69    SRA Co-op.Housing Society Ltd.    156-F/242                         |
     |-----------------------------------------------------------------------------|
 11. |   69    SRA Co-op.Housing Society Ltd.    156-F/243                         |
 12. |   69    SRA Co-op.Housing Society Ltd.    156-F/244                         |
 13. |   69    SRA Co-op.Housing Society Ltd.    156-F/245                         |
 14. |   69    SRA Co-op.Housing Society Ltd.    156-F/246                         |
 15. |   69    SRA Co-op.Housing Society Ltd.    156-F/247                         |
     |-----------------------------------------------------------------------------|
 16. |   70   Jai Bhavani CHS Ltd. (Proposed)                                      |
 17. |   71                   Bhavani Housing                156-B/70              |
 18. |   71                   Bhavani Housing                156-B/71              |
 19. |   71                   Bhavani Housing                156-B/72              |
 20. |   71                   Bhavani Housing                156-B/73              |
     |-----------------------------------------------------------------------------|
 21. |   71                   Bhavani Housing                156-B/74              |
 22. |   71                   Bhavani Housing                156-B/75              |
 23. |   71                   Bhavani Housing                156-B/76              |
 24. |   71                   Bhavani Housing                156-B/77              |
 25. |   71                   Bhavani Housing                156-B/78              |
     |-----------------------------------------------------------------------------|
 26. |   71                   Bhavani Housing                156-B/79              |
 27. |   71                   Bhavani Housing                156-B/80              |
 28. |   71                   Bhavani Housing                156-B/81              |
 29. |   71                   Bhavani Housing                156-B/82              |
 30. |   71                   Bhavani Housing                156-B/83              |
     |-----------------------------------------------------------------------------|
 31. |   71                   Bhavani Housing                156-B/84              |
 32. |   71                   Bhavani Housing                156-B/85              |
 33. |   71                   Bhavani Housing                156-B/86              |
 34. |   71                   Bhavani Housing                156-B/87              |
 35. |   71                   Bhavani Housing                156-B/88              |
     |-----------------------------------------------------------------------------|
 36. |   71                   Bhavani Housing                156-B/89              |
 37. |   72           Shivshakti SRA CHS Ltd.                                      |
 38. |   73        Shree Ram CHS Ltd. (Prop.)                                      |
 39. |   74              Ram CHS Ltd. (Prop.)                            156-D/158 |
 40. |   74              Ram CHS Ltd. (Prop.)                            156-D/159 |
     |-----------------------------------------------------------------------------|
 41. |   74              Ram CHS Ltd. (Prop.)                            156-D/160 |
 42. |   74              Ram CHS Ltd. (Prop.)                            156-D/161 |
 43. |   74              Ram CHS Ltd. (Prop.)                            156-D/162 |
 44. |   74              Ram CHS Ltd. (Prop.)                            156-D/163 |
 45. |   74              Ram CHS Ltd. (Prop.)                            156-D/164 |
     |-----------------------------------------------------------------------------|
 46. |   74              Ram CHS Ltd. (Prop.)                            156-D/165 |
 47. |   74              Ram CHS Ltd. (Prop.)                            156-D/166 |
 48. |   74              Ram CHS Ltd. (Prop.)                            156-D/167 |
 49. |   74              Ram CHS Ltd. (Prop.)                            156-D/168 |
 50. |   74              Ram CHS Ltd. (Prop.)                            156-D/169 |
     +-----------------------------------------------------------------------------+

@Nick Cox的注释:

我还没有研究问题,只是您的解决方案。以下版本包括一些简化。采取你想要的,而忽略其余的。

clear

input int SRNo str200 SchemeName str30 CTSNo3 str15 CTSNo4 str15 CTSNo9
69 "SRA Co-op.Housing Society Ltd." " 156-F/233 - 47" ""
70 "Jai Bhavani CHS Ltd. (Proposed)" "7 (Pt.)" ""
71 "Bhavani Housing" " "  "156-B/70 - 89 " ""
72 "Shivshakti SRA CHS Ltd." "364 ‘A’" ""
73 "Shree Ram CHS Ltd. (Prop.)" " " ""
74 "Ram CHS Ltd. (Prop.)" " " " " " 156-D/158 - 69 "
end

list

preserve

generate tag = !strmatch(CTSNo3, "*/*") & !strmatch(CTSNo4, "*/*") & ///
                   !strmatch(CTSNo9, "*/*")
keep if tag 
save base, replace

restore
unab CTSvars : CTSNo* 

foreach var of local CTSvars {
    preserve

    generate tag = strmatch(`var', "*/*")
    keep if tag 

    generate part0 = substr(`var', 1, strpos(`var', "/"))
    generate part1 = substr(`var', strpos(`var', "/") + 1, strpos(`var', "-") - 1)
    generate part2 = substr(`var', strrpos(`var', "-") + 1, .)

    if strlen(trim(part1)) == 3 {
        generate xpa = abs(real(substr(part1, 2, .)) - real(part2)) + 1 
    }
    else {
        generate xpa = abs(real(part1) - real(part2)) + 1
    }

    expand xpa
    bysort SRNo (`var'): generate N`var' = part0 + string(real(part1) + _n - 1)
    drop tag part0 part1 part2 xpa 

    list
    save `var', replace
    restore
}

use base, clear
append using `CTSvars'
sort SRNo NCTS*
list SRNo SchemeName NCTS*

编辑:

CTSNo变量中有多个观察值时,代码的以下部分:

if strlen(trim(part1)) == 3 {
    generate xpa = abs(real(substr(part1, 2, .)) - real(part2)) + 1 
}
else {
    generate xpa = abs(real(part1) - real(part2)) + 1
}

应替换为:

drop tag
generate tag = strlen(trim(part1))

generate xpa = .
replace xpa = abs(real(part1) - real(part2)) + 1
replace xpa = abs(real(substr(part1, 2, .)) - real(part2)) + 1 if tag == 3

例如,如果您的CTSNo4变量如下所示:

     +----------------------------------------------------------+
     | SRNo                        SchemeName            CTSNo4 |
     |----------------------------------------------------------|
  1. |   69    SRA Co-op.Housing Society Ltd.                   |
  2. |   70   Jai Bhavani CHS Ltd. (Proposed)                   |
  3. |   71                   Bhavani Housing    156-B/70 - 89  |
  4. |   72           Shivshakti SRA CHS Ltd.                   |
  5. |   73        Shree Ram CHS Ltd. (Prop.)                   |
     |----------------------------------------------------------|
  6. |   74              Ram CHS Ltd. (Prop.)                   |
  7. |   75               CHS Society Housing   113-Z/160 - 72  |
     +----------------------------------------------------------+