如何清除一行数字中的第一个单元格,如果第一个单元格为 0 - 使用 Microsoft Excel 或 Stata
How to clear the first cell in a row of numbers, if that first cell is 0 - with Microsoft Excel or Stata
我有 3000 多支共同基金的历史 return。
其中很多基金的 return 历史记录都以 0 开头;然而,并非所有人都这样做。
如果历史 return 的序列以 0 开头,则该 0 需要 deleted/replaced 加一个空格 space。
我的数据看起来像这样
1),0,5,2,7,43,7,9,23,7,9
2),6,3,21,5,3,2,6,2,4
3),0,9,2,4,7,2,4,56,7,3,5,4,1
4),0,2,8,9,3,4,6,1,3,3
5),7,0,4,2,4,6,7,4,2,5,7,7
逗号分隔不同的单元格。因此,对于第 1)、3) 和 4) 行,我需要删除 return 序列开始的 0。 2) 和 6) 需要保持不变。
我该怎么做?我更喜欢 Excel 中的解决方案,但如果需要我可以使用 Stata。
在 Stata 中有两种方法可以做到这一点。
clear
input byte(v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 v21 v22 v23 v24 v25 v26 v27 v28)
. . . . . . . . . . . . . . 0 5 2 7 43 7 9 23 7 9 . . . .
. . . . . . . . . . . . . . . . . . . 6 3 21 5 3 2 6 2 4
. . . . . . . 0 9 2 4 7 2 4 56 7 3 5 4 1 . . . . . . . .
. . . . . . . . . . . . . . . . . 0 2 8 9 3 4 6 1 3 3 .
. . . . . . . . . . . 7 0 4 2 4 6 7 4 2 5 7 7 . . . . .
end
tempfile f
save "`f'"
list v1-v21, noobs compress
* The looping method
gen nomis = 0
qui foreach v of varlist v* {
replace nomis = nomis + 1 if !mi(`v')
replace `v' = . if `v' == 0 & nomis == 1
count if nomis == 0
if r(N) == 0 continue, break
}
list v1-v21, noobs compress
* this is easier to do with data in long form
use "`f'", clear
gen id = _n
reshape long v, i(id) j(n)
bysort id (n): gen nomis = sum(!mi(v)) // this is a running sum!
replace v = . if v == 0 & nomis == 1
drop nomis
reshape wide v, i(id) j(n)
list v1-v21, noobs compress
我有 3000 多支共同基金的历史 return。
其中很多基金的 return 历史记录都以 0 开头;然而,并非所有人都这样做。
如果历史 return 的序列以 0 开头,则该 0 需要 deleted/replaced 加一个空格 space。
我的数据看起来像这样
1),0,5,2,7,43,7,9,23,7,9
2),6,3,21,5,3,2,6,2,4
3),0,9,2,4,7,2,4,56,7,3,5,4,1
4),0,2,8,9,3,4,6,1,3,3
5),7,0,4,2,4,6,7,4,2,5,7,7
逗号分隔不同的单元格。因此,对于第 1)、3) 和 4) 行,我需要删除 return 序列开始的 0。 2) 和 6) 需要保持不变。
我该怎么做?我更喜欢 Excel 中的解决方案,但如果需要我可以使用 Stata。
在 Stata 中有两种方法可以做到这一点。
clear
input byte(v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 v21 v22 v23 v24 v25 v26 v27 v28)
. . . . . . . . . . . . . . 0 5 2 7 43 7 9 23 7 9 . . . .
. . . . . . . . . . . . . . . . . . . 6 3 21 5 3 2 6 2 4
. . . . . . . 0 9 2 4 7 2 4 56 7 3 5 4 1 . . . . . . . .
. . . . . . . . . . . . . . . . . 0 2 8 9 3 4 6 1 3 3 .
. . . . . . . . . . . 7 0 4 2 4 6 7 4 2 5 7 7 . . . . .
end
tempfile f
save "`f'"
list v1-v21, noobs compress
* The looping method
gen nomis = 0
qui foreach v of varlist v* {
replace nomis = nomis + 1 if !mi(`v')
replace `v' = . if `v' == 0 & nomis == 1
count if nomis == 0
if r(N) == 0 continue, break
}
list v1-v21, noobs compress
* this is easier to do with data in long form
use "`f'", clear
gen id = _n
reshape long v, i(id) j(n)
bysort id (n): gen nomis = sum(!mi(v)) // this is a running sum!
replace v = . if v == 0 & nomis == 1
drop nomis
reshape wide v, i(id) j(n)
list v1-v21, noobs compress