- data chk;*_null_;
- array a [-100000 : 100000];*_temporary_;
- ** allocate sample array with random numbers, about 10 percent duplicate,给array赋予随机数值;
- do _q = lbound (a) to hbound (a);
- a [_q] = ceil (ranuni (1) * 2000000);
- end;
- ** set sort parameters;
- seq = 'A'; * A = ascending(由小到大), D = descending(由大到小);
- nodupkey = 0; * 0 = duplicates allowed(允许有重复), 1 = duplicates not allowed(不允许有重复);
- dcl hash _hh (hashexp: 0, ordered: seq) ;*建立 hash table;
- dcl hiter _hi ('_hh');
- _hh.definekey ('_k', '_n'); * _n – extra enumerating key;
- _hh.definedata ('_k'); * _k automatically assumes array data type;
- _hh.definedone ( );
- ** load composite (_k _n) key on the table;*将array a里的随机数值,引入到hash table里的_k, _n内;
- ** if duplicates to be retained, set 0 <- _n;
- do _j = lbound (a) to hbound (a);
- _n = _j * ^ nodupkey;
- _k = a [_j];
- _hh.replace();
- end;
- ** use iterator HI to reload array from HH table, now in order;*排序!;
- _n = lbound (a) - 1;
- do _rc = _hi.first() by 0 while ( _rc = 0 );
- _n = _n + 1;
- a [_n] = _k;
- _rc = _hi.next();
- end ;
- _q = _n;
- ** fill array tail with missing values if duplicates are delete;*重复删减者给予missing values;
- do _n = _q + 1 to hbound (a);
- a [_n] = .;
- end ;
- *drop _:; * drop auxiliary variables: _j, _k, _n, _q, _rc等变量;
- ** check if array is now sorted;*假定都已sort,并标记为1!;
- sorted = 1;
- do _n = lbound (a) + 1 to _q while ( sorted );
- if a [_n - 1] > a [_n] then sorted = 0 ;*检测是否已经排序,并最后标记!如果有按由小到大未排序好的,就标为0;
- end;
- put sorted=;*打印显示出结果;
- proc print data=chk(obs=200);run;
1。先建立一个临时array叫_temporary_。
2。然后,给array赋予随机数值。
3。这个程序里,大概一般人不太熟悉的就是这第三部分的hash table的用法!如果有谁对此不很清楚,劳驾,请先阅读一下有关SAS hash table的内容和用法了!然后,再参看原程序中的英文注释!
4。之所以在此用hash table,我想或是为了快速方便下面的sort,因为hash table的方法里具有排序功能,简便易用?或是同时演示一下如何应用hash table吧?
5。当sorting结束,进行一下检测,然后,打印出检测结果来验证是否已sort,sort=1意思是已sort好,nodupkey=0意思是:允许有重复!
6。整个程序执行结束。


雷达卡



京公网安备 11010802022788号







