评估字符串分数

时间:2018-11-16 19:29:50

标签: sas

假设我具有以下数据集:

/dataEnvelope/payload[@class]/@class

我想了解这个:

/dataEnvelope/payload/@class

我知道我可以通过以下操作获得此输出:

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

但是我不想对所有内容进行硬编码。我知道我可以在Python中执行以下操作:

frac
0.33
0.25
0.42
1.00
0.00
0.58

4 个答案:

答案 0 :(得分:2)

类似下面的使用扫描和输入的方法应该起作用。

 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;

答案 1 :(得分:2)

这就是我要这样做的结果,即结果是数字,并且尚未在字符之间来回转换,如果您在单个步骤中通过RESOLVE使用%SYSEVALF会发生这种情况。

filename FT76F001 temp;
data _null_;
   file FT76F001;
   input frac $;
   put +3 'x=' frac ';' frac=$quote. '; output;';
   datalines;
1/3
1/4
5/12
1
0
7/12
;
run;

data frac;
   length x 8 frac $16;
   %inc FT76F001;
   run;
proc print;
   run;

enter image description here

这是使用SYSEVALF

464  data _null_;
465     input frac $;
466     x = input(resolve(cats('%sysevalf(',frac,')')),f32.);
467     put 'NOTE: ' (_all_)(=);
468     datalines;

NOTE: frac=1/3 x=0.3333333333
NOTE: frac=1/4 x=0.25
NOTE: frac=5/12 x=0.4166666667
NOTE: frac=1 x=1
NOTE: frac=0 x=0
NOTE: frac=7/12 x=0.5833333333

答案 2 :(得分:2)

我要说的最简单的方法是将您的分数值放入宏变量,在其上调用sysevalf函数以计算该值,最后将其转换回普通变量。这样做还有一个好处,就是可以使用任何数学表达式,而不仅仅是分数。

类似的东西:

data test;
    set df;

    call symput('myMacroVariable',frac);            /* Put the value of frac into a macro variable */
    dec = resolve('%sysevalf(&myMacroVariable)');   /* Evaluate the value of the macro variable */
run;

编辑:别听我说,data_null_的回答只是一行而已。

答案 3 :(得分:2)

data df;
input frac $;
_frac=put(scan(frac,1)/coalesce(scan(frac,2),1),4.2);
datalines;
1/3
1/4
5/12
1
0
7/12
;
run;
相关问题