加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

SAS中的HASH语句及其常见应用

(2014-03-27 19:55:16)

http://blog.csdn.net/yugao1986/article/details/6929892

首先,应用HASH有以下几点优点

• Key lookup occurs in memory, avoiding costly disk access.
• When a key lookup occurs, only a small subset of the records are searched.
• The key and data parts of a record can consist of more than one value, removing the need to format and concatenate values to construct the key and data parts.
• The hash object allocates memory as records are added. That is, the hash object only allocates as much memory as it needs and the number of records that can be stored is only limited by the amount of memory available to SAS.
• When loading a hash object from a data set, the data set need not be sorted or indexed.

下面介绍如何定义hash对象

The hash table methods are the functions it can perform, namely:

 

1.HASH OBJECT LOAD AND LOOKUP

  1. data participants;  
  2. input name gender:$1. treatment $;  
  3. datalines;  
  4. John Placebo  
  5. Ronald Drug-A  
  6. Barbara Drug-B  
  7. Alice Drug-A  
  8.  
  9. data weight(drop=i);  
  10. input date:DATE9. @;  
  11. do i 1 to 4;  
  12. input name weight @;  
  13. output;  
  14. end;  
  15.   
  16. datalines;  
  17. 05May2006 Barbara 125 Alice 130 Ronald 170 John 160  
  18. 04Jun2006 Barbara 122 Alice 133 Ronald 168 John 155  
  19.  
  20. data results;  
  21.    length name treatment gender 1;  
  22.    if _N_ 1 then do;  
  23.    declare hash h(dataset:'participants');   
  24.       h.defineKey('name');                   
  25.       h.defineData('gender', 'treatment');   
  26.       h.defineDone();                        
  27.    end;  
  28.    set weight;  
  29.    if h.find() then output;  
  30. run;  
  31. proc print data=results 
  32.    format date DATE9.;  
  33.    var date name gender weight treatment;  
  34. run;  

2.ADD, REPLACE, AND OUTPUT

  1. data goals;  
  2. input player when $9.;  
  3. datalines;  
  4. Hill 1st 01:24  
  5. Jones 1st 09:43  
  6. Santos 1st 12:45  
  7. Santos 2nd 00:42  
  8. Santos 2nd 03:46  
  9. Jones 2nd 11:15  
  10.  
  11. data _null_;  
  12.    length goals_list 64;  
  13.    if _N_ 1 then do;  
  14.    declare hash h();  
  15.       h.defineKey('player');  
  16.       h.defineData('player', 'goals_list');  
  17.       h.defineDone();  
  18.    end;  
  19.    set goals end=done 
  20.    if h.find() ^= then do;    
  21.       goals_list when 
  22.       h.add();                  
  23.    end;  
  24.    else do;  
  25.       goals_list trim(goals_list) || ', || when;  
  26.       h.replace();              
  27.    end;  
  28.    if done then h.output(dataset:'goal_summary');   
  29. run;  
  30. proc print data=goal_summary 
  31. run;  

HASH的应用

1.HITER: HASH ITERATOR OBJECT

  1. data sample  
  2. input sat  
  3. cards  
  4. 185 01  
  5. 971 02  
  6. 400 03  
  7. 260 04  
  8. 922 05  
  9. 970 06  
  10. 543 07  
  11. 532 08  
  12. 050 09  
  13. 067 10  
  14.  
  15. run  
  16.   
  17. data _null_  
  18.    if then set sample  
  19.    dcl hash hh dataset: 'sample', hashexp: 8, ordered: 'a')  
  20.    dcl hiter hi 'hh'  
  21.    hh.DefineKey 'k'  
  22.    hh.DefineData 'sat' 'k'  
  23.    hh.DefineDone ()  
  24.    do rc hi.first () by while rc 0  
  25.       put k z3+1 sat z2 
  26.       rc hi.next ()   
  27.    end  
  28.    put 13 '-'  
  29.    do rc hi.last () by while rc 0  
  30.       put k z3+1 sat z2 
  31.       rc hi.prev ()  
  32.    end  
  33.    stop  
  34. run  
  35.   
  36.   
  37. data _null_  
  38.    array (-100000 100000) _temporary_  
  39.    array (-100000 100000) _temporary_  
  40.    do j lbound (a) to hbound (a)  
  41.       (j) ceil ranuni (1) 1e5  
  42.       (j)  
  43.    end  
  44.    length ka sb  
  45.    declare hash hh (hashexp: 0, ordered: ‘a’  
  46.    declare hiter hi 'hh'  
  47.            hh.DefineKey 'ka'  
  48.            hh.DefineData 'ka' 'sb'  
  49.            hh.DefineDone ()  
  50.    do j lbound(a) to hbound(a)  
  51.       ka a (j)  
  52.    if hh.check () then continue  
  53.       sb b (j)  
  54.       n_unique ++  
  55.       hh.add ()  
  56.    end  
  57. sort ascending  
  58.    rc hi.first ()  
  59.    do j lbound (a) by while rc 0  
  60.       (j) ka  
  61.       (j) sb  
  62.       rc hi.next ()  
  63.    end  
  64. sort descending  
  65.    rc hi.last()  
  66.    do j lbound(a) by while rc 0  
  67.       (j) ka  
  68.       (j) sb  
  69.       rc hi.prev()  
  70.    end  
  71.    stop  
  72. run  

2.Summarizing Without Summary

 

  1. data input  
  2.    do k1 1e6 to by -1   
  3.       k2 put (k1, z7.)  
  4.       do num 1 to ceil (ranuni(1) 6)  
  5.          output  
  6.       end  
  7.    end  
  8. run  
  9.   
  10. proc summary data input nway  
  11.    class k1 k2  
  12.    var num  
  13.    output out summ_sum (drop _:) sum sum  
  14. run  
  15.   
  16. data _null_  
  17.    if then set input  
  18.    dcl hash hh (hashexp:16)  
  19.        hh.definekey ('k1', 'k2'  
  20.        hh.definedata ('k1', 'k2', 'sum')  
  21.        hh.definedone ()  
  22.    do until (eof)  
  23.    set input end eof  
  24.    if hh.find () ne then sum 0  
  25.       sum ++ num  
  26.       hh.replace ()  
  27.    end  
  28.    rc hh.output (dataset: 'hash_sum')  
  29. run  

3.SPLITTING A SAS FILE DYNAMICALLY USING THE .OUTPUT() METHOD

  1.   
  2. data sample  
  3. input id transid amt  
  4. cards  
  5. 11 40  
  6. 11 26  
  7. 12 97  
  8. 13  
  9. 13  
  10. 14 22  
  11. 14  
  12. 15 43  
  13. 15 81  
  14. 11 86  
  15. 11 85  
  16.  
  17. run  
  18.   
  19. proc sql noprint  
  20.    select distinct 'OUT' || put (id, best.-l)  
  21.    into dslist  
  22.    separated by  
  23.    from sample  
  24.    select 'WHEN (' || put (id, best.-l) || ') OUTPUT OUT' || put (id, best.-l)  
  25.    into whenlist  
  26.    separated by ';'  
  27.    from sample;  
  28. quit  
  29. proc sort data sample  
  30.    by id transid amt  
  31. run  
  32. data &dslist  
  33.    set sample  
  34.    select id  
  35.    &whenlist  
  36.    otherwise  
  37.    end;  
  38. run  
  39.   
  40. data _null_  
  41.    dcl hash hid (ordered: 'a')  
  42.        hid.definekey ('id', 'transid', 'amt', '_n_')  
  43.        hid.definedata ('id', 'transid', 'amt'  
  44.        hid.definedone  
  45.    do _n_ 1 by until last.id  
  46.    set sample  
  47.    by id  
  48.    hid.add()  
  49.    end  
  50.    hid.output (dataset: 'OUT' || put (id, best.-l))  
  51. run  
  52.   
  53.   
  54. data sample  
  55. input id transid amt  
  56. cards  
  57. 11 86  
  58. 14 22  
  59. 12 97  
  60. 14  
  61. 15 43  
  62. 13  
  63. 13  
  64. 11 40  
  65. 15 81  
  66. 11 85  
  67. 11 26  
  68.  
  69. run  
  70. data _null_  
  71.    dcl hash hoh (ordered: 'a')  
  72.    dcl hiter hih ('hoh'  
  73.        hoh.definekey ('id'  
  74.        hoh.definedata ('id', 'hh'  
  75.        hoh.definedone ()  
  76.    dcl hash hh ()  
  77.    do _n_ 1 by until eof  
  78.       set sample end eof  
  79.       if hoh.find () ne then do  
  80.          hh _new_ hash (ordered: 'a')  
  81.          hh.definekey ('id','transid', '_n_')  
  82.          hh.definedata ('id','transid', 'amt')  
  83.          hh.definedone ()  
  84.          hoh.replace ()  
  85.       end  
  86.       hh.replace()  
  87.    end  
  88.    do rc hih.next () by while rc 0  
  89.       hh.output (dataset: 'out'|| put (id, best.-L))  
  90.       rc hih.next()  
  91.    end  
  92.    stop  
  93. run  

声明:文章主要摘录《Hash Component Objects:Dynamic Data Storage and Table Look-Up》和《Data Step Hash Objects as Programming Tools》

相关文献:

《How to Implement the SAS® DATA Step Hash Object》

《An Introduction to SAS® Hash Programming Techniques》

《Getting Started with the DATA Step Hash Object》

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有