演示脚本
input str20 x
"50%"
""
"5%"
"5%,15%,%60"
end
* 1
gen y = real(regexr(x,"%","")) / 100
* 3
split x,parse(",") gen(z)
replace y = real(regexr(z1,"%","")) / 100 ///
+real(regexr(z2,"%","")) / 100 ///
+real(regexr(z3,"%","")) / 100 ///
if y == .
* 2
replace y = 1 if y == .
des
l
执行结果:
. input str20 x
x
1. "50%"
2. ""
3. "5%"
4. "5%,15%,%60"
5. end
.
. * 1
. gen y = real(regexr(x,"%","")) / 100
(2 missing values generated)
.
. * 3
. split x,parse(",") gen(z)
variables created as string:
z1 z2 z3
. replace y = real(regexr(z1,"%","")) / 100 ///
> +real(regexr(z2,"%","")) / 100 ///
> +real(regexr(z3,"%","")) / 100 ///
> if y == .
(1 real change made)
.
. * 2
. replace y = 1 if y == .
(1 real change made)
.
. des
Contains data
obs: 4
vars: 5
size: 148
----------------------------------------------------------------------------------------
storage display value
variable name type format label variable label
----------------------------------------------------------------------------------------
x str20 %20s
y double %10.0g
z1 str3 %9s
z2 str3 %9s
z3 str3 %9s
----------------------------------------------------------------------------------------
Sorted by:
Note: Dataset has changed since last saved.
. l
+------------------------------------+
| x y z1 z2 z3 |
|------------------------------------|
1. | 50% .5 50% |
2. | 1 |
3. | 5% .05 5% |
4. | 5%,15%,%60 .8 5% 15% %60 |
+------------------------------------+
.
end of do-file
|