将字符串数字表达式转换为数字

时间:2018-03-08 05:32:35

标签: stata

我有一个数据集,其中包含{" 1/2"," 0.5"}等字符串表达式。如何将这些字符串转换为实际数字?我知道有a -> a -> a但只能转换"数字字符串"数字; "数字表达式"被遗失了。

1 个答案:

答案 0 :(得分:1)

好问题。请注意,real()在此处没有帮助。这是一个粗略的解决方案:

clear 
input str6 myproblem 
"1 / 2"
"0.5"
"ln(3)" 
end 

gen onesolution = .
quietly forval i = 1/`=_N' { 
    replace onesolution = `= myproblem[`i']' in `i'
} 

list 

     +---------------------+
     | myprob~m   onesol~n |
     |---------------------|
  1. |    1 / 2         .5 |
  2. |      0.5         .5 |
  3. |    ln(3)   1.098612 |
     +---------------------+