|
|
楼主

楼主 |
发表于 2010-11-7 22:28:04
|
只看该作者
跟crackman读SAS程序(92)--hash迭代器的一个实例
From crackman's blog on Baidu
<p>data astro;<br>
input obj $1-4 ra $6-12 dec $14-19;<br>
datalines;<br>
M31 00 42.7 +41 16<br>
M71 19 53.8 +18 47<br>
M51 13 29.9 +47 12<br>
M98 12 13.8 +14 54<br>
M13 16 41.7 +36 28<br>
M39 21 32.2 +48 26<br>
M81 09 55.6 +69 04<br>
M100 12 22.9 +15 49<br>
M41 06 46.0 -20 44<br>
M44 08 40.1 +19 59<br>
M10 16 57.1 -04 06<br>
M57 18 53.6 +33 02<br>
M3 13 42.2 +28 23<br>
M22 18 36.4 -23 54<br>
M23 17 56.8 -19 01<br>
M49 12 29.8 +08 00<br>
M68 12 39.5 -26 45<br>
M17 18 20.8 -16 11<br>
M14 17 37.6 -03 15<br>
M29 20 23.9 +38 32<br>
M34 02 42.0 +42 47<br>
M82 09 55.8 +69 41<br>
M59 12 42.0 +11 39<br>
M74 01 36.7 +15 47<br>
M25 18 31.6 -19 15<br>
;<br>
run;<br>
data out;<br>
if _N_ = 1 then do;<br>
length obj $10;<br>
length ra $10;<br>
length dec $10;<br>
/*用HASH存储数据集SATRO中的数据*/ </p>
<p>declare hash h(dataset:"work.astro", ordered: 'yes');<br>
/* Define variables RA and OBJ as key and data for hash object */<br>
declare hiter iter('h');</p>
<p>/*定义了一个迭代器HITER*/<br>
h.defineKey('ra');<br>
h.defineData('ra', 'obj');<br>
h.defineDone();<br>
call missing(obj, ra, dec);<br>
end;<br>
rc = iter.first();/*RC是一个指示变量,也就是每一次迭代之后看看剩下的有没有数据观测,有点像一个排队买烧饼吃,老板先让大家排好队,按照年龄大小排一个队,这里的按照年龄排队有点像HASH的KEY,然后每一个的位次就是value.队伍排好后,每一次老板都会问一下,你要不要,不要就下一个,当每次走了一个人之后,新队伍的第一个人继续被老板问,又像first person吧,那么这个第一个人是队伍里面年龄最小或者最大的,重复次过程,那么RC就是老板问你要不要烧饼你的回答,是就是0不是就是非0值。*/<br>
do while (rc = 0);<br>
if ra GE '12' then<br>
output;<br>
rc = iter.next();<br>
end;<br>
run;<br>
proc print data=work.out;<br>
var ra obj;<br>
title 'Messier Objects Greater than 12 Sorted by Right Ascension Values';<br>
run;</p>
<p>以下是官方对FIRST方法的解释:</p>
<p><a name="a002901988"></a>The FIRST method returns<font style="background-color: #ffff00" color="#000000"> <strong>the first data item in the hash object</strong>.</font> If you use the <span class="strong">ordered: 'yes'</span> or <span class="strong">ordered: 'ascending'</span> argument tag in the DECLARE statement or _NEW_ operator when you instantiate the hash object, then <strong><font style="background-color: #ffff00">the data item that is returned is the one with the 'least' key</font></strong> (smallest numeric value or first alphabetic character), because the data items are sorted in ascending key-value order in the hash object. Repeated calls to the NEXT method will iteratively traverse the hash object and return the data items in ascending key order. Conversely, if you use the <span class="strong">ordered: 'descending'</span> argument tag in the DECLARE statement or _NEW_ operator when you instantiate the hash object, then the data item that is returned is the one with the 'highest' key (largest numeric value or last alphabetic character), because the data items are sorted in descending key-value order in the hash object. Repeated calls to the NEXT method will iteratively traverse the hash object and return the data items in descending key order.</p> <a href="http://hi.baidu.com/crack%5Fman/blog/item/31d062646c53a454eaf8f80d.html">阅读全文</a>
<br/><b>类别:</b><a href="http://hi.baidu.com/crack%5Fman/blog/category/%B8%FAcrackman%B6%C1sas%B3%CC%D0%F2">跟crackman读sas程序</a> <a href="http://hi.baidu.com/crack%5Fman/blog/item/31d062646c53a454eaf8f80d.html#comment">查看评论</a> |
|