SAS9新体验-关于DATA STEP的使用
不说那么多了,呵,真高兴,今天装上SAS 9.1.2了 小试了一个小小的技巧,还挺管用的 不知你有没有碰到这样的麻烦,比如在遍历数据集时需要打开一个字典表进行匹配搜索,你可能会这样做 ?%let n=1000; ?data _null_; ?set 要遍历的数据集; ?arrary arr1{&n}; ?if _n_=1 then do; ??装载字典表数据到数组arr1 ?end; ?.... ?匹配当前记录到字典表 ?run; ?555,真郁闷,那数组开多大才好呢,其实也可以这样解决,把字典表的记录条数存在一个宏变量中去,就算这样,也够麻烦的,还有匹配要自己写呢,要是写的不好,说不定速度就下降了,闷死了 ?呵呵,不过现在可好了,SAS终于提供了一种终极解决方案,就是提供了2个可以在DATA STEP中使用的对象,分别为 ?Hash object 、Hash Iterator Object ?不用说了吧,用过C++的人就知道了,那个HASH对象用来存储数据,Iterator对象就用来遍历hash对象中的数据了,OK,就是这样简单 ?好了,来个例子吧,引用别人的,不是我的,不过测试是通过的 /* Richard A. DeVenezia ?* www.devenezia.com ?* Feb 11, 2003 ?* ?* Show the values of the data items of a DATA Step hash object in the log ?*/
%macro putHash (hash, vars);
? %* ? %* hash - variable that references a hash object ? %* vars - space separated list of variables linked to the data items of hash ? %*??????? separate with pound sign (#) to get varname=varvalue format ? %*;
? %* generate a random variable name; ? %local random hi rc; ? %let random = %substr(%sysfunc(ranuni(0),10.8),3); ? %let hi = hi_&random; ? %let rc = rc_&random;
? %* emit DATA Step code that iterates the hash and ? %* puts the data items values in the log;
? declare hiter &hi ("&hash"); ? do &rc = &hi..first() by 0 while (&rc = 0); ??? put %sysfunc(translate(&vars,=,#)); ??? &rc = &hi..next(); ? end; ? &hi..delete(); ? put;
? %put WARNING: Values of variables &vars will change;
%mend;
data _null_; ? * prep the PDV, this lazy way is not always recommended; ? if 0 then set sashelp.class(obs=0);
? declare hash H (dataset:'sashelp.class'); ? H.defineKey ('Name'); ? H.defineData('Name', 'Age', 'Weight', 'Sex'); ? H.defineDone();
? %putHash (H,name#age#weight#sex#); ? %putHash (H,name age weight sex);
? stop; run; ?看到输出结果了吧,还有什么不明白么?实在不知道就去看SAS帮助吧,很EASY的 
|