计算字符串分数 [英] Evaluate string fraction

查看:24
本文介绍了计算字符串分数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下数据集:

data df;
input frac $;
datalines;
1/3
1/4
5/12
1
0
7/12
;
run;

我想了解这个:

frac
0.33
0.25
0.42
1.00
0.00
0.58

我知道我可以通过这样做得到这个输出:

I know I could get this output by doing this:

proc sql;
select
  case
    when frac = '1/12' then 0.083
    when frac = '1/6' then 0.167
    ...
  end as frac_as_num
from df
;
quit;

但我宁愿不对所有内容进行硬编码.我知道我可以在 Python 中做这样的事情:

But I'd rather not hard-code everything. I know I could do something like this in Python:

frac = ['1/12', '1/6', ...]
[eval(f) for f in frac]

推荐答案

像下面这样使用扫描和输入应该可以工作.

something like below using scan and input should work.

 proc sql;
 create table want as 
 select frac, 
    case when index(frac,'/') then 
    input(scan(frac, 1),best32.)/input(scan(frac, 2),best32.) 
    else input(frac,best32.) end as frac_as_num format= 5.2
from df;

这篇关于计算字符串分数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆