proc fcmp outlib=work.functions.samples; /*SAS_AGE自定义的功能函数存放的地方*/
function sas_age(Birthday,Current); /*函数的两个参数*/
age=int(intck('month',birthday,current)/12); /*用INTCK计算AGE*/
if month(birthday)=month(current) then
age = age-(day(birthday)>day(current)); /*如果月份一样,如果生日的天数大于当前的天数,那么返回1,否则是0*/
return(age); /*输出AGE这个结果,也就是函数的返回值吧*/
endsub;
run;
options cmplib=work.functions; /*注意要加上这个OPTIONS*/
data birth;
input name $ bday :mmddyy10.;
datalines;
Miguel 12/31/1973
Joe 02/28/1976
Rutger 03/29/1976
Broguen 03/01/1976
Susan 12/12/1976
Michael 02/14/1971
LeCe 11/09/1967
Hans 07/02/1955
Lou 07/30/1960
;
run;
data ages;
set birth;
retain current;
if _n_=1 then current=today();
format bday current worddate20.;
actualage=sas_age(bday,current);
run;
这个FCMP过程给了我们很多可以自定义函数的空间,大大扩充SAS函数的自定义,值得学习 |