全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
4997 26
2013-01-27
先进先出原则在金融和会计领域有广泛的应用,但是一直没有找到SAS关于这方面的程序,故提问如下:

Type

Quantity

Price

Cost

Buy

100

$1.20

$120.00

Buy

50

$1.60

$80.00

Sell

(120)

 

($152.00)

Sell

(12)

 

($19.20)

Buy

15

$1.10

$16.50

Buy

40

$1.20

$48.00

Sell

(70)

 

($89.70)

Buy

50

$1.15

$57.50

Sell

(30)

 

($34.65)

Sell

(10)

 

($11.50)

上表是一件商品的买卖记录。商家先后买入了100和50件商品,然后卖出120件,安装先进先出的原则,卖出的商品中分别来自于第一次购买的100件和第二次购买的50件中的20件,因此,实现的销售成本是100*1.2+20*1.6=152,之后的以此类推
请问上述功能应该如何用SAS实现?谢谢
二维码

扫码加我 拉你入群

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

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

全部回复
2013-1-27 16:21:06
data test;
    infile cards missover;
    input Type $ Quantity  Price : dollar12.2  Cost : dollar12.2 ;
        format price cost dollar12.2;
        cards;
Buy 100  $1.20  $120.00
Buy 50  $1.60  $80.00
Sell 120
Sell 12
Buy 15  $1.10  $16.50
Buy 40  $1.20 $48.00
Sell 70
Buy 50  $1.15  $57.50
Sell 30
Sell 10
;

data test1;
   set test(keep=type quantity);
   where type="Sell";
   q_out=quantity;
   do while (q_out>0);
      if q_in<=0 or _n_=1 then set test(where=(_type="Buy") rename=(quantity=q_in type=_type));
          if q_out>q_in then do;
                          cost1=sum(cost1,price*q_in);
                         q_out=q_out-q_in;
                         q_in=0;
                end;
          else do;
                 cost1=sum(cost1,price*q_out);
                 q_in=q_in-q_out;
                 q_out=0;
                 p_in=price;
          end;
   end;
run;

     
data wanted;
    set test;
        if type="Sell" then do;
                set test1 (keep= type cost1);
               cost=cost1;
        end;
        drop cost1;
run;
二维码

扫码加我 拉你入群

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

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

2013-1-27 23:07:05
pobel 发表于 2013-1-27 16:21
data test;
    infile cards missover;
    input Type $ Quantity  Price : dollar12.2  Cost : dollar ...
继续问一下,如果是多种商品的话就应该写循环了吧,比如有个id来区分是商品1还是商品2,这个循环应该怎么写呢?如
id

Type

Quantity

Price

1

Buy

100

$1.20

1

Buy

50

$1.60

1

Sell

(120)

 

1

Sell

(12)

 

1

Buy

15

$1.10

1

Buy

40

$1.20

1

Sell

(70)

 

1

Buy

50

$1.15

1

Sell

(30)

 

1

Sell

(10)

 

2

Buy

100

$1.20

2

Sell

(70)

 

2

Buy

50

$1.15

2

Sell

(30)

 

2

Sell

(10)

 

二维码

扫码加我 拉你入群

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

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

2013-1-28 01:07:00
denver 发表于 2013-1-27 23:07
继续问一下,如果是多种商品的话就应该写循环了吧,比如有个id来区分是商品1还是商品2,这个循环应该怎么 ...
code not tested. Hopefully it works. Jingju

复制代码


二维码

扫码加我 拉你入群

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

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

2013-1-28 08:52:35
data test;
    infile cards missover;
    input id $ Type $ Quantity  Price : dollar12.2  Cost : dollar12.2 ;
        format price cost dollar12.2;
        cards;
1 Buy 100  $1.20  $120.00
1 Buy 50  $1.60  $80.00
1 Sell 120
1 Sell 12
1 Buy 15  $1.10  $16.50
1 Buy 40  $1.20 $48.00
1 Sell 70
1 Buy 50  $1.15  $57.50
1 Sell 30
1 Sell 10
2 Buy 100  $1.20
2 Sell 70
2 Buy 50 $1.15
2 Sell 30&iexcl;&iexcl;
2 Sell 10
;

data test1;
   set test(keep=id type quantity);
   where type="Sell";
   by id;
   q_out=quantity;
   do while (q_out>0);
      if q_in<=0 or first.id then set test(where=(_type="Buy") rename=(id=_id quantity=q_in type=_type));
              if id=_id;
          if q_out>q_in then do;
                          cost1=sum(cost1,price*q_in);
                         q_out=q_out-q_in;
                         q_in=0;
                end;
          else do;
                 cost1=sum(cost1,price*q_out);
                 q_in=q_in-q_out;
                 q_out=0;
                 p_in=price;
          end;
   end;
run;

     
data wanted;
    set test;
        if type="Sell" then do;
                set test1 (keep= type cost1);
               cost=cost1;
        end;
        drop cost1;
run;
二维码

扫码加我 拉你入群

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

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

2013-1-28 09:59:35
我也写了个程序~
data table;
input type $ quantity price;
CARDS;
Buy 100 1.2
Buy 50 1.6
Sell 120 .
Sell 12 .
Buy 15 1.1
Buy 40 1.2
Sell 70 .
Buy 50 1.15
Sell 30 .
Sell 10 .
;
RUN;

data table;
set table;
if type='Buy' then cost=price*quantity;
n=_n_;
run;

data buy_surplus sell;
set table;
if type='Buy' then output buy_surplus;
else output sell;
run;

%macro sell_cost(quantity,n);
data buy_surplus;
set buy_surplus;
sum_quantity+quantity;
sum_cost+cost;
if sum_quantity>=&quantity. and lag(sum_quantity)<&quantity. then do;
quantity=sum_quantity-&quantity.;
cost=price*quantity;
sellcost=sum_cost-price*quantity;
call symput("sellcost_&n.",sellcost);
end;
if sum_quantity>&quantity.;
drop sum_quantity sum_cost sellcost;
run;

data table;
set table;
if n=&n. then cost=&&sellcost_&n.;
run;
%mend sell_cost;

data test;
set sell;
macro=cats('%sell_cost(',quantity,',',n,');');
call execute(macro);
run;
二维码

扫码加我 拉你入群

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

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

点击查看更多内容…
相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

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