请选择 进入手机版 | 继续访问电脑版
楼主: qwbyzw
2066 1

[问答] 请教sas中,proc fcmp 自定的函数怎么设置默认值和不固定参数? [推广有奖]

  • 0关注
  • 0粉丝

初中生

85%

还不是VIP/贵宾

-

威望
0
论坛币
8 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
549 点
帖子
4
精华
0
在线时间
35 小时
注册时间
2014-10-7
最后登录
2020-8-18

qwbyzw 发表于 2017-10-27 16:56:24 |显示全部楼层 |坛友微信交流群

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
在sas中可以在fcmp里面自己写函数, 但遇到一些疑问想请教一下:
  • 怎么让函数有默认值, 宏函数里面是可以通过变量赋值让宏变量有默认值的, 但在自定的函数中行不通
  • 怎么让函数接收不定个数的参数, 类似于系统的函数: compress() 一样, 可以接收 1~3 个参数, cats() 应该是可以接收无限个参数的. 这个功能在宏函数里面也可以通过parmbuff实现, 但是怎么才能在自定义函数里面实现呢?
请各位指教
  1. proc fcmp outlib=work.xxx.xxx;
  2.     function test(v=3);
  3.         put v=;   
  4.     endsub;
  5. run;
复制代码



二维码

扫码加我 拉你入群

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

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

关键词:FCMP ROC FCM CMP function

sas9.4 发表于 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;

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-3-28 18:40