全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
2434 1
2017-10-27
在sas中可以在fcmp里面自己写函数, 但遇到一些疑问想请教一下:
  • 怎么让函数有默认值, 宏函数里面是可以通过变量赋值让宏变量有默认值的, 但在自定的函数中行不通
  • 怎么让函数接收不定个数的参数, 类似于系统的函数: compress() 一样, 可以接收 1~3 个参数, cats() 应该是可以接收无限个参数的. 这个功能在宏函数里面也可以通过parmbuff实现, 但是怎么才能在自定义函数里面实现呢?
请各位指教
复制代码



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2017-11-10 01:41:40
proc fcmp  outlib=SASHELP.SLKWXL.FINANCE  LIBRARY=(SASHELP.SLKWXL);
function amordegrc_slk(cost,datep,fperiod,salvage,period,rate,basis)
          label="Excel AMORDEGRC";
/*-----------------------------------------------------------------
  * ENTRY:     amordegrc_slk
  *
  * PURPOSE:   Microsoft Excel's AMORDEGRC function. Returns the
  *            depreciation for each accounting period. Similar to
  *            amorlinc function, except that a depreciation
  *            coefficient is applied.
  *
  * USAGE:     amordegrc = amordegrc_slk(cost,datep,fperiod,salvage,
  *                                    period,rate,basis);
  *               cost - cost of the asset.
  *               datep - date of the purchase of the asset.
  *               fperiod - date of the end of the first period.
  *               salvage - salvage value at the end of the life
  *                  of the asset.
  *               period - the period.
  *               rate - the rate of depreciation.e
  *               basis - type of day count basis.
  *                   basis = 0: US (NASD) 30/360.
  *                   basis = 1: Actual/actual.
  *                   basis = 2: NOT ALLOWED.*                   basis = 3: Actual/365.
  *                   basis = 4: European 30/360.
  *
  * NOTES:  The depreciation coefficient is different depending one
  *         the number of years in the life of the asset being
  *         depreciated. The life of asset is the inverse
  *         of rate:
  *
  *        rate           life of asset   depreciation coeff (DC)
  *        0.25-0.333     3-4 years       1.5
  *        0.1666-0.2     5-6 years       2.0
  *        < 0.1666       >6 years        2.5
  *
  *        The function will return depreciation until the last
  *        period of the life of the asset, or until the
  *        accumulated value of depreciation is greater than the
  *        cost of the asset minus the salvage value.
  *
  *     For period 0:
  *        cost*rate*DC*yearfrac(datep, fperiod, basis);
  *          where yearfrac() calculates the fraction
  *          of the year represented by the whole days between
  *          the start_date and end_date.
  *
  *     For periods 1 through lifeofasset - 2:
  *        (cost - depreciation from previous periods)*rate*DC;
  *
  *     For the period of lifeofasset - 1 through
  *                 the last period of asset's life:
  *        (cost - depreciation from previous periods)/2;
  *
  *     When the accumulated value of depreciation exceeds the cost
  *     of the asset minus the salvage value, then the function
  *     returns zero.
  *
  *-----------------------------------------------------------------*/
    life = 1/rate;
    if life > 0 & life < 1 then return(.);
    if life > 1 & life < 2 then return(.);
    if life > 2 & life < 3 then return(.);
    if life > 4 & life < 5 then return(.);
    if life > 3 & life < 4 then coeff = 1.5;
    if life > 5 & life < 6 then coeff = 2;
    if life > 6 then coeff = 2.5;
    select(basis);
       when(0)
       do;
           B = 360;
           DPF = datdif(datep, fperiod,'30/360');
       end;
       when(1)
       do;
           newyeard = intnx('year', datep - 1, 0);
           endyeard = intnx('year', datep - 1, 1) - 1;
           B = endyeard - newyeard + 1;
           DPF = datdif(datep, fperiod, 'act/act');
       end;
       when(3)
       do;
          B = 365;
          DPF = datdif(datep, fperiod, 'act/365');
       end;
       when(4)
       do;
          B = 360;
          DPF = datdif4_slk(datep, fperiod);
       end;
       otherwise
                 return(.);
    end;
    depr0 = cost*rate*coeff*DPF/B;
    if period = 0 then return(depr0);
    if period > life then return(0);
    cumdep = depr0;
    cms = cost - salvage;
    cost = cost - depr0;
    n = 1;
    if int(period) <= int(life) - 2 then
    do;
       do while(n <= period & cumdep <= cms );
          tempdep = cost*rate*coeff;
          cost = cost - tempdep;
          cumdep + tempdep;
          n + 1;
       end;
       depr = tempdep;
       if cumdep > cms & period >= n then return(0);
       else
            return(depr);
    end;
    if int(period) >= int(life) - 1 then
    do;
       ind = int(life) - 1;
       do while(n < ind & cumdep <= cms );
          tempdep = cost*rate*coeff;
          cost = cost - tempdep;
          cumdep + tempdep;
          n + 1;
       end;
       deprl = cost/2;
       if int(period) = int(life) - 1 then
       do;
           if cumdep > cms & period >= n then return(0);
           else
                return(deprl);
       end;
       else
            if int(period) = int(life) then
       do;
           if salvage < deprl & cumdep <= cms then return(deprl);
           else
                return(0);
       end;
    end;
    return(deprl);

endsub;
run;
quit;
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群