楼主: denver
5122 26

关于先进先出(FIFO)的SAS实现 [推广有奖]

11
tangliang0905 发表于 2013-1-28 12:34:53
pobel 发表于 2013-1-28 11:44
data test;
    infile cards missover;
    input id $ Type $ Quantity  Price : dollar12.2  Cost ...
看来用hash方法非常简便,一开始我光顾着用data方法却会显得十分复杂,看来还是要好好学习hash啊

12
tangliang0905 发表于 2013-1-28 13:34:00
pobel 发表于 2013-1-28 11:44
data test;
    infile cards missover;
    input id $ Type $ Quantity  Price : dollar12.2  Cost ...
学习了一下hash的方法,关键步骤在declare了hash之后,可以调用那步                            rc=stack.remove();
_n+(-1);
以及
rc=stack.replace();
所以可不可以这样理解,
因为FIFO的方法是和sas本身读入观测量的顺序是一样的,所以直接就从第一个观测值开始自上而下进行循环就可以了,这样就用一般的data方法就可以了。
但是LIFO方法有点反其道而行之,必须要找到SELL的每一个观测值,然后由该观测值自下而下反规律读取数据进行操作,如果用正常的data方法,就会显得非常臃肿复杂,而采取hash的方法,可以任意的自上而下或者自下而上对数据进行替换或者删除,就显得比较简洁明了了。
这样能不能请楼主再稍微简要介绍一下hash和一般data步骤的区别,或者什么时候该采取hash方法呢?

13
pobel 在职认证  发表于 2013-1-29 08:25:31
tangliang0905 发表于 2013-1-28 13:34
学习了一下hash的方法,关键步骤在declare了hash之后,可以调用那步                            rc=stac ...
你可以参考以下几篇关于hash的文章:

http://www2.sas.com/proceedings/forum2008/029-2008.pdf
http://www2.sas.com/proceedings/forum2008/095-2008.pdf
http://support.sas.com/resources/papers/proceedings09/084-2009.pdf



和谐拯救危机

14
tangliang0905 发表于 2013-1-29 11:52:44
pobel 发表于 2013-1-29 08:25
你可以参考以下几篇关于hash的文章:

http://www2.sas.com/proceedings/forum2008/029-2008.pdf
多谢多谢

15
zhangzachary 发表于 2013-1-29 17:30:43
这样的好帖总是好晚才看到。最近忙,等有空了再来训练自己!~
寒冰凤凰 My blog: http://blog.sina.com.cn/u/1058955485

16
denver 发表于 2013-1-30 01:10:05
pobel 发表于 2013-1-29 08:25
你可以参考以下几篇关于hash的文章:

http://www2.sas.com/proceedings/forum2008/029-2008.pdf
找到了解决这个问题的一个技术文档,也是用hash,不过我用我的数据跑了一下,许多数据跑不过,不知道啥原因。你的hash程序还没仔细看呢,感觉hash语法很是不爽,没有data步来的直接
http://www.nesug.org/Proceedings/nesug11/fi/fi03.pdf
Denver大家一起读Paper系列索引贴:
https://bbs.pinggu.org/thread-1430892-1-1.html

17
tangliang0905 发表于 2013-1-30 03:01:08
denver 发表于 2013-1-30 01:10
找到了解决这个问题的一个技术文档,也是用hash,不过我用我的数据跑了一下,许多数据跑不过,不知道啥原 ...
是什么原因跑不过啊?我这几天正好恶补一下hash,觉得可能对于小data看不出优势来,但是对于大型data来说,hash还是很有他的优势的额,而且它可以和data步合在一起使用,至少可以帮助data步拓展一下思路啊。至少这个LIFO过程中光用data步的,逻辑判断太多了,比较复杂。

18
jingju11 发表于 2013-1-30 07:30:23
tangliang0905 发表于 2013-1-30 03:01
是什么原因跑不过啊?我这几天正好恶补一下hash,觉得可能对于小data看不出优势来,但是对于大型data来说 ...
这往往取决与如何去想这个问题.如果用ARRAY,应该很简单,从后往前数即可
  1. do i =n to 1 by _1;
复制代码
京剧

19
pobel 在职认证  发表于 2013-1-30 08:46:01
denver 发表于 2013-1-30 01:10
找到了解决这个问题的一个技术文档,也是用hash,不过我用我的数据跑了一下,许多数据跑不过,不知道啥原 ...
解决问题的方法往往不是唯一的,Data步,数组,hash,SQL,究竟选用哪种工具,和最初的思路以及个人编程习惯有关系。个人认为用hash来表示堆栈还是很形象的。
和谐拯救危机

20
pobel 在职认证  发表于 2013-1-30 09:05:32
数组的方法:

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
1 Sell 13
2 Buy 100  $1.20
2 Sell 70
2 Buy 50 $1.15
2 Sell 30
2 Sell 10
;
data test1;
    set test;
     by id;
     array q(100) _temporary_;
     array p(100) _temporary_;

     if first.id then _n=0;

     if type="Buy" then do;
                 _n+1;
                 q(_n)=quantity;
                 p(_n)=price;
     end;
     else if type="Sell" then do;
                 q_out=quantity;
                do while(q_out>0);
                          if q_out>=q(_n) then do;
                                           cost1=sum(cost1,q(_n)*p(_n));
                                           q_out=q_out-q(_n);
                                           _n+(-1);
                          end;
                         else do;
                                       cost1=sum(cost1,p(_n)*q_out);
                                       q(_n)=q(_n)-q_out;
                                       q_out=0;
                          end;
                end;
        end;
       drop _n q_out;
run;

和谐拯救危机

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-1 04:37