Q:

Evaluate string fraction

Suppose I have the following dataset:
data df;
input frac $;
datalines;
1/3
1/4
5/12
1
0
7/12
;
run;

And I want to get to this:
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;

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]

A:

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;

