使用foreach剪切样本时出错

时间:2019-03-28 08:19:45

标签: foreach stata stata-macros

我想使用foreach使用Stata剪切相同的样本。

我编写了以下代码段:

foreach i of numlist 0/11 {
preserve
keep id projectno nickname
gen start=`i'*30000+1
gen end=(`i'+1)*30000
outsheet using d:\profile\nickname_`i'.xls in `start'/`end'
restore
}

但是,尽管多次修改,我仍收到以下错误:

  

'/'无效的观察编号

如何纠正我的代码?

2 个答案:

答案 0 :(得分:1)

in命令中的outsheet语句是错误的,因为startend是作为变量和 not 局部宏生成的。您需要按照以下步骤初始化startend

local start = `i' * 30000 + 1
local end = (`i' + 1) * 30000

使用Stata的auto玩具数据集考虑以下玩具示例:

sysuse auto, clear

foreach i of numlist 0/11 {
    preserve
    keep price mpg make
    local start = (`i' * 3) + 1
    local end = (`i' + 1) * 3
    list in `start' / `end'
    restore
}

结果:

     +---------------------------+
     | make          price   mpg |
     |---------------------------|
  1. | AMC Concord   4,099    22 |
  2. | AMC Pacer     4,749    17 |
  3. | AMC Spirit    3,799    22 |
     +---------------------------+

     +-----------------------------+
     | make            price   mpg |
     |-----------------------------|
  4. | Buick Century   4,816    20 |
  5. | Buick Electra   7,827    15 |
  6. | Buick LeSabre   5,788    18 |
     +-----------------------------+

     +------------------------------+
     | make             price   mpg |
     |------------------------------|
  7. | Buick Opel       4,453    26 |
  8. | Buick Regal      5,189    20 |
  9. | Buick Riviera   10,372    16 |
     +------------------------------+

     +------------------------------+
     | make             price   mpg |
     |------------------------------|
 10. | Buick Skylark    4,082    19 |
 11. | Cad. Deville    11,385    14 |
 12. | Cad. Eldorado   14,500    14 |
     +------------------------------+

     +-------------------------------+
     | make              price   mpg |
     |-------------------------------|
 13. | Cad. Seville     15,906    21 |
 14. | Chev. Chevette    3,299    29 |
 15. | Chev. Impala      5,705    16 |
     +-------------------------------+

     +---------------------------------+
     | make                price   mpg |
     |---------------------------------|
 16. | Chev. Malibu        4,504    22 |
 17. | Chev. Monte Carlo   5,104    22 |
 18. | Chev. Monza         3,667    24 |
     +---------------------------------+

     +------------------------------+
     | make             price   mpg |
     |------------------------------|
 19. | Chev. Nova       3,955    19 |
 20. | Dodge Colt       3,984    30 |
 21. | Dodge Diplomat   4,010    18 |
     +------------------------------+

     +-------------------------------+
     | make              price   mpg |
     |-------------------------------|
 22. | Dodge Magnum      5,886    16 |
 23. | Dodge St. Regis   6,342    17 |
 24. | Ford Fiesta       4,389    28 |
     +-------------------------------+

     +----------------------------------+
     | make                 price   mpg |
     |----------------------------------|
 25. | Ford Mustang         4,187    21 |
 26. | Linc. Continental   11,497    12 |
 27. | Linc. Mark V        13,594    12 |
     +----------------------------------+

     +---------------------------------+
     | make                price   mpg |
     |---------------------------------|
 28. | Linc. Versailles   13,466    14 |
 29. | Merc. Bobcat        3,829    22 |
 30. | Merc. Cougar        5,379    14 |
     +---------------------------------+

     +-----------------------------+
     | make            price   mpg |
     |-----------------------------|
 31. | Merc. Marquis   6,165    15 |
 32. | Merc. Monarch   4,516    18 |
 33. | Merc. XR-7      6,303    14 |
     +-----------------------------+

     +------------------------------+
     | make             price   mpg |
     |------------------------------|
 34. | Merc. Zephyr     3,291    20 |
 35. | Olds 98          8,814    21 |
 36. | Olds Cutl Supr   5,172    19 |
     +------------------------------+

请注意,命令preservekeeprestore不必在循环中,因为它们是一次性操作,重复执行效率很低。

答案 1 :(得分:1)

这不是一个完整的答案-侧重于您问题的附带问题-但这并不容易成为评论。

连同其他地方解释的更改,我将命令的顺序更改为

preserve
keep id projectno nickname

forval i = 0/11 {
    local start= `i' * 30000 + 1
    local end = (`i' + 1) * 30000
    outsheet using d:\profile\nickname_`i'.xls in `start'/`end'
}

restore