data one_week ;
input BP $ NAME $ nameI $ ABB $ DSET ;
attrib dset format= ddmmyy. informat= ddmmyy. ;
cards ;
10000001 Jack J. CAMPAIGN 160710
10000001 Jack J. CAMPAIGN 230710
10000001 Jack J. CAMPAIGN 300710
10000001 Jack J. CAMPAIGN 100810
10000001 Jack J. CAMPAIGN 130810
10000006 Karen K NORMAL 230710
10000006 Karen K NORMAL 300710
10000006 Karen K NORMAL 100810
10000006 Karen K NORMAL 130810
10000007 Albert A CAMPAIGN 160710
10000007 Albert A CAMPAIGN 230710
10000007 Albert A NORMAL 300710
10000008 Lisa L CAMPAIGN 230710
10000008 Lisa L CAMPAIGN 300710
10000008 Lisa L CAMPAIGN 100810
10000008 Lisa L CAMPAIGN 130810
10000009 Kal L CAMPAIGN 230710
10000009 Kal L NORMAL 300710
10000009 Kal L NORMAL 100810
10000009 Kal L CAMPAIGN 130810
10000009 Kal L SPECIAL 150910
;
proc sort ;
by bp dset ;
run ;
data changes ;
changes= 0;
do until(last.bp) ;
set one_week ;
by bp abb notsorted ;
changes +(first.abb and not first.bp) ;
end ;
if changes ; /*这句话是排除没有变化的观测对象*/
keep bp name: changes abb dset;
rename abb = latest_abb dset=latest_dset ;
run;
这个里面注意几个地方:
1.DO UNTIL循环对ONE_WEEK的每一个观测进行计算CHANGES
2.黄色标注的地方,FIRST.ABB和NOT FIRST.BP,确定变化的ABB
3.IF CHANGES 就是CHANGES不为0或者缺失均为入选的观测对象
4.KEEP里面的 name:后面的冒号其实就是保留前缀为NAME的变量。
5.specifies that observations with the same BY value are grouped together but are not necessarily sorted in alphabetical or numeric order. 这里的NOTSORTED是起到分组的功能而不是排序的功能。