周末在写作业实在抽不出时间更新sql,后续找个机会就一些实例做一次分享讲解,今天就先在这里总结一下hash对象的基本知识。

sql server sa 哈希值解密 sas 哈希函数_数据集

hash对象的基本原理

hash表(又称哈希表),是根据键值(Key value)直接进行访问的数据结构。也就是说,它通过把键值映射到表中一个位置来访问记录,以加快查找的速度。这个映射函数叫做hash函数,存放记录的数组叫做hash表。

那么hash对象可以实现哪些功能呢?概括来说,可以实现查询、增删、连接、汇总统计等功能,大多数基础的功能都是可以实现。

hash对象的优点
• Key lookup occurs in memory, avoiding costly disk access.
简单来说最主要的功能就是提升查询的效率。
hash对象的定义&初始化
方法1:declare hash variable_name(argument_tag-1 : value-1 <, ...argument_tag-n: value-n>);
variable_name = _new_ hash(argument_tag-1: value-1 <, ...argument_tag-n: value-n>);
方法2:declare hash hashname();
hash对象方法
definekey 定义键
definedata 定义值
definedone 定义完成
add 添加键值,如已存在该键,则忽略
replace 如hash表中不存在该键,则添加,反之,则替换
remove 删除键值对
find 查找键值, 如存在则将值写入对应变量
output 将hash表输出到数据集
clear 清空hash表,但并不删除对象
equal 判断两个hash类是否相等
hash的对象属性:
item_size 返回hash对象中元素的大小(type)
num_items 返回数目
hash遍历器
hiter定义:
declare hiter myiter('h');
hiter对象方法:
first 将遍历器定位到hash表中第一条观测
next 将遍历器定位到hash表中下一条观测
“ 实操讲解 ”
实操1:使用hash进行表连接
data test_hash;
length bank_id  bank_name $20.;/*定义hash表的字段,其中数值型的可不用写长度,但字符型需要*/
    if _n_ = 1 then do;/*因为在使用hash对象前需要对其进行定义和声明,但是这项工作只需要完成一次,所以把第一行观测单独“拎”出来,进行定义声明*/
    declare hash h(dataset:'a.bank_info' ,ordered:'ascending');/*其中h为新建hash表的名称,dataset为处理的数据集*/
    h.definekey('bank_id');/*定义键,为dataset中的主键,当key为多个时,用逗号隔开*/
    h.definedata('bank_name','bank_id');/*定义值,为dataset中的值*/
    /*如果需要使用全部值,可通过h.definedata(all:'yes')来进行全部的定义*/
    h.definedone();/*定义完成标志*/
    call missing(bank_name);
end;
set a.account;
call missing(bank_name);
if h.find(key:bank_Id)=0 then output;/*若key为多个时,用空格隔开*/
run;
实操2:使用hash进行数据集输出
data a_;
if _n_=0 then set sashelp.class;
if _n_=1 then do;
declare hash h(dataset:'sashelp.class', ordered:'a');
h.definekey('name');
h.definedata(all:'yes');
h.definedone();
end;
h.output(dataset:'test');
run;
实操3:使用hash进行数据添加
data _null_;
length d $20;
length k $20;
/* Declare the hash object and key and data variables */
if _N_ = 1 then do;
   declare hash h(hashexp: 4);
   rc = h.defineKey('k');
   rc = h.defineData('d');
   rc = h.defineDone();
end;
/* Define constant value for key and data */
k = 'Homer';
d = 'Odyssey';
/* Use the ADD method to add the key and data to the hash object */
rc = h.add();
if (rc ne 0) then put 'Add failed.';
/* Define constant value for key and data */
k = 'Joyce';
d = 'Ulysses';
/* Use the ADD method to add the key and data to the hash object */
rc = h.add();
if (rc ne 0) then put 'Add failed.';
k = 'Homer';
/* Use the FIND method to retrieve the data associated with 'Homer' key */
rc = h.find();
if (rc = 0) then put d=;
else put 'Key Homer not found.';
run;
实操4:使用hash进行数据查找
data match;
   length k 8;
   length s 8;
   if _N_ = 1 then do;
      /* load SMALL data set into the hash object */
     declare hash h(dataset: "work.small", hashexp: 6);
      /* define SMALL data set variable K as key and S as value */
      h.defineKey('k');
      h.defineData('s');
      h.defineDone();
      /* avoid uninitialized variable notes */
      call missing(k, s);
   end;
/* use the SET statement to iterate over the LARGE data set using */
/* keys in the LARGE data set to match keys in the hash object */
set large;
rc = h.find();
if (rc = 0) then output;
run;
实操5:使用hash进行数据删除
data _null_;
length d $20;
length k $20;
/* Declare the hash object and key and data variables */
if _N_ = 1 then do;
   declare hash h(hashexp: 4);
   rc = h.defineKey('k');
   rc = h.defineData('d');
   rc = h.defineDone();
end;
/* Define constant value for key and data */
k = 'Joyce';
d = 'Ulysses';
/* Use the ADD method to add the key and data to the hash object */
rc = h.add();
if (rc ne 0) then
   put 'Add failed.';
/* Define constant value for key and data */
k = 'Homer';
d = 'Odyssey';
/* Use the ADD method to add the key and data to the hash object */
rc = h.add();
if (rc ne 0) then
   put 'Add failed.';
/* Use the REPLACE method to replace 'Odyssey' with 'Iliad' */
k = 'Homer';
d = 'Iliad';
rc = h.add();
if (rc = 0) then
   put d=;
else
   put 'Replace not successful.';
/* Use the REMOVE method to remove the 'Joyce' key and data */
k = 'Joyce';
rc = h.remove();
if (rc = 0) then
   put k 'removed from hash object';
else
   put 'Deletion not successful.';
run;

sql server sa 哈希值解密 sas 哈希函数_hash表_02

备注:案例来自网络及sas帮助文档。