DATA _null_;
cost = 4.99;
units = 3;
ucost = Round(cost/units,.01);
PUT cost units ucost ;
RUN;
DATA _null_;
x = 234.2357222;
x2 = Round(x, .25); * the nearest quarter;
x3 = Round(x, .05); * the nearest nickel;
x4 = Round(x, 5); * the nearest 5 dollars;
Put x x2 x3 x4;
RUN;
DATA _null_;
dt = dhms('12mar2003'D,14,23,10); * create a datetime - 2:23pm;
dt2 = Round(dt,hms(0,15,00)); * round it to nearest 15 minutes;
dt3 = Round(dt,hms(0,7,30)); * round it to nearest 7 1/2 minutes;
dt4 = Round(dt,hms(0,0,15)); * to the nearest 15 seconds;
Put (dt dt2 dt3 dt4) (DATETIME. ' ');
RUN;
DATA _null_;
dt = '12mar2003:14:23:10'DT; * create a datetime - 2:23pm;
dt2 = Round(dt,'0:15:00'T); * round it to nearest 15 minutes;
dt3 = Round(dt,'0:7:30'T); * round it to nearest 7 1/2 minutes;
dt4 = Round(dt,'0:00:15'T); * to the nearest 15 seconds;
Put (dt dt2 dt3 dt4) (DATETIME. ' ');
RUN;
我们经常用ROUND函数,主要是指定的小位数。
第一个DATA _NULL_,结果为
4.99 3 1.66
第二个DATA _NULL_,结果为
234.2357222 234.25 234.25 235
其实这个时候是把0.25 0.05 5作为小位数修正一个间隔距离了,例如对于0.25来说,234.2357222 应该是和234.00 234.25 234.50 看看234.2357222在这三个数比较靠近谁,就输出。同样对于0.05来说,234.2357222应该和234.05 234.10 234.15 234.20 234.25 234.30这样间隔0.05的数字进行比较,最后就是234.25了。其他一样
这个程序实际对应为货币计算的,下面的为对应时间的计算,不过注意表达 一个HMS函数,一个用的是T格式。
|