楼主: Tigflanker
1540 8

[问答] 问一个Hash问题:有无函数能从hash table中取值? [推广有奖]

  • 8关注
  • 18粉丝

副教授

49%

还不是VIP/贵宾

-

威望
0
论坛币
2321 个
通用积分
9.8528
学术水平
179 点
热心指数
194 点
信用等级
167 点
经验
27443 点
帖子
622
精华
0
在线时间
851 小时
注册时间
2011-3-27
最后登录
2023-5-14

150论坛币
我想改问个问题,不需要涉及统计疑问,是关于hash table的:
  1. data a;
  2.   input a b $;
  3.   cards;
  4. 1 q
  5. 2 w
  6. 3 e
  7. 1 r
  8. 2 t
  9. ;run;

  10. data b;
  11.   if 0 then set a;
  12.   if _n_ = 1 then do;
  13.     length new $1.;
  14.     dcl hash h(hashexp:4);
  15.         h.definekey('a');
  16.         h.definedata('a','b','new');
  17.         h.definedone();
  18.         call missing(a,b,new);
  19.   end;

  20.   set a end = last;

  21.   if h.find() then do;
  22.     new = b;
  23.     h.add();
  24.   end;else do;  /* when _n_ = 4*/
  25.     new = /* value b already saved in hash library:'q'*/
  26.     h.add();
  27.   end;
  28. run;
复制代码
描述:目前我有一个其实很多变量的数据集,我只是列举了一个变量A,然后数据集不能排序。

问题是,按照PDV的顺序载入一张空hash table,当我的key第一次被添加时,hash table的b值为‘q’;
我想问下,当我的key第一次被查找到时,我怎么将hash table中的值‘q'拿出来,反馈到我的PDV中?

谢谢。

最佳答案

yongyitian 查看完整内容

楼主要找的就是 find() 方法 (在hash里叫方法). h.find()在查找到key的同时, 会 set data (把data的值取出, 覆盖PDV中相同变量的值) c=h.check()只查找key的值是否存在,不管data的值.
关键词:Table HASH ABLE Has tab 相关性

本帖被以下文库推荐

Bye SAS.
若有缘,能重聚。
沙发
yongyitian 发表于 2014-11-15 14:32:15 |只看作者 |坛友微信交流群
楼主要找的就是  find() 方法 (在hash里叫方法).
h.find()在查找到key的同时, 会 set data (把data的值取出, 覆盖PDV中相同变量的值)
c=h.check()只查找key的值是否存在,不管data的值.

  1. /* try this */
  2. data a;
  3.   input a b $;
  4.   cards;
  5. 1 q
  6. 2 w
  7. 3 e
  8. 1 r
  9. 2 t
  10. ;run;

  11. data b;
  12.   if 0 then set a;
  13.   if _n_ = 1 then do;
  14.     length new $4.;
  15.     declare hash h(hashexp:4);
  16.         h.definekey('a');
  17.         h.definedata('a','b','new');
  18.         h.definedone();
  19.         call missing(a,b,new);
  20.   end;
  21.   set a end = last;
  22.     c = h.check(); put _all_;
  23.   if  c ^= 0 then do;       /* c^=0 mean the key not exist in the hash table */  
  24.     new = b;
  25.     h.add();
  26.   end;
  27.     else do;  /* when _n_ = 4*/
  28.               *  new = ; /* value b already saved in hash library:'q'*/
  29.     h.find();           /* assign b with the value in hash table */
  30. *    new = b;
  31.   end;
  32. run;
复制代码

已有 1 人评分论坛币 学术水平 热心指数 信用等级 收起 理由
Tigflanker + 5 + 3 + 3 + 3 所言极是,非常感谢!:)

总评分: 论坛币 + 5  学术水平 + 3  热心指数 + 3  信用等级 + 3   查看全部评分

使用道具

藤椅
Tigflanker 发表于 2014-11-15 22:52:28 |只看作者 |坛友微信交流群
问题已改,自己顶下,谢谢

使用道具

板凳
Tigflanker 发表于 2014-11-19 09:23:29 |只看作者 |坛友微信交流群
yongyitian 发表于 2014-11-15 14:32
楼主要找的就是  find() 方法 (在hash里叫方法).
h.find()在查找到key的同时, 会 set data (把data的值取 ...
我还想问您个可能比较基础的问题。

就是我们在data步的时候,有用:if 0 then set xxx来利用纯编译读取变量信息到PDV。

那我问下,我有时想建一张空的hash table,需要包含数据集xxx的所有变量属性,然后从PDV往里写东西;
除了h.definedata(all:'y'); + h.clear();还有其他办法吗?
关键是9.1.3中没有h.clear方法,我咋想不到啥其他好的方法呢?。

使用道具

报纸
yongyitian 发表于 2014-11-19 21:12:13 |只看作者 |坛友微信交流群
Tigflanker 发表于 2014-11-19 09:23
我还想问您个可能比较基础的问题。

就是我们在data步的时候,有用:if 0 then set xxx来利用纯编译读取 ...
定义hash时,只要没有 load 数据集,那么建立好的 hash table 就是空的.
但是这样做的时候要在 definekey 和 definedata 语句中列出所有变量.
因为没有数据集,所以 all: 'y' 就不能用了.
变量多的话可以用宏变量.
  1. data new_class;
  2.   if 0 then set class;    /* set all variable contribute */
  3.    if _n_ = 1 then do;
  4.       declare hash h ();
  5.       h.definekey('name');
  6.       h.definedata('name', 'sex', 'age', 'weight', 'height');
  7.       h.definedone();
  8.     end;
  9.   rc=h.output(dataset: 'empty_hash');
  10. run;
复制代码
另一个方法是用 iterator 和 remove(). 下面是按照 sas document 中的例子修改的. 并不简单.
  1. data new_class;
  2.    if 0 then set class;   /* set all variable contribute */
  3. /*    define hash table */
  4.    if _n_ = 1 then do;
  5.      declare hash h (dataset: 'class');
  6.      h.definekey('name');
  7.      h.definedata(all: 'y');
  8.      h.definedone();
  9.    end;

  10.    declare hiter hi("h");
  11.      length name_key $10;
  12.    do while(hi.next()=0);
  13.      if flag then rc=h.remove(key:name_key);
  14.       if ^missing(name) then do;
  15.          name_key = name;
  16.              flag = 1;
  17.       end;
  18.    end;
  19.    rc=h.remove();
  20.    rc=h.output(dataset: 'empty_hash2');
  21.   /* the hash is empty now */
  22. run;
复制代码

使用道具

地板
farmman60 发表于 2014-11-20 01:06:50 |只看作者 |坛友微信交流群
Tigflanker 发表于 2014-11-19 09:23
我还想问您个可能比较基础的问题。

就是我们在data步的时候,有用:if 0 then set xxx来利用纯编译读取 ...
  1. Try to make a empty table by SQL, then load it.

  2. proc sql;
  3.   create table class like sashelp.class;
  4.   quit;

  5. data _null_;
  6.    if 0 then set class;    /* set all variable contribute */
  7.    if _n_ = 1 then do;
  8.       declare hash h (dataset:'class');
  9.       h.definekey('name');
  10.       h.definedata(all:'y');
  11.       h.definedone();
  12.    end;
  13. name='Paul';
  14. sex='M';
  15. age=12;
  16. weight=56;
  17. height=110;
  18. h.add();
  19. rc=h.output(dataset: 'new_class');
  20. run;
复制代码
已有 2 人评分论坛币 学术水平 热心指数 信用等级 收起 理由
Tigflanker + 5 + 3 + 3 + 3 观点有启发
yongyitian + 5 + 2 + 2 + 2 精彩帖子

总评分: 论坛币 + 10  学术水平 + 5  热心指数 + 5  信用等级 + 5   查看全部评分

使用道具

7
farmman60 发表于 2014-11-21 05:53:03 |只看作者 |坛友微信交流群
farmman60 发表于 2014-11-20 01:06
  1. It is better, I just find out.

  2. data _null_;
  3.    if 0 then set sashelp.class(obs=0);    /* set all variable contribute */
  4.    if _n_ = 1 then do;
  5.       declare hash h (dataset:'sashelp.class(obs=0)');
  6.       h.definekey('name');
  7.       h.definedata(all:'y');
  8.       h.definedone();
  9.    end;
  10. name='Paul';
  11. sex='M';
  12. age=12;
  13. weight=56;
  14. height=110;
  15. h.add();
  16. rc=h.output(dataset: 'new_class');
  17. run;
复制代码
已有 1 人评分论坛币 学术水平 热心指数 信用等级 收起 理由
Tigflanker + 5 + 3 + 3 + 3 Cool way!!

总评分: 论坛币 + 5  学术水平 + 3  热心指数 + 3  信用等级 + 3   查看全部评分

使用道具

8
Tigflanker 发表于 2014-11-21 10:21:07 |只看作者 |坛友微信交流群
farmman60 发表于 2014-11-21 05:53
这方法确实非常好!

可惜在sas9.1.3还是不能用,没想到第一次体会到9.1.3到9.2的差异这么大。。

使用道具

9
Tigflanker 发表于 2014-11-21 10:21:09 |只看作者 |坛友微信交流群
farmman60 发表于 2014-11-21 05:53
这方法确实非常好!

可惜在sas9.1.3还是不能用,没想到第一次体会到9.1.3到9.2的差异这么大。。

使用道具

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

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

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

GMT+8, 2024-4-20 23:45