- data test;
- input type $1. @;
- retain address;
- if type='H' then do;
- if _n_>1 then output;
- total=0;
- input address $3-17;
- end;
- else if type='P' then total+1;
- cards;
- H zd2
- H sdf
- P mark
- P mark
- H a21a
- P mark
- H a21z
- P mark
- P mark
- P mark
- H a21sdf
- ;
- run;
上面的程序运行结果:
obs type address total
1 H zd2 0
2 H sdf 0
3 H a21a 1
4 H a21z 3
上面的程序,type=H代表地址,P代表的是在它之前的这个地址的详细信息。
程序是为了统计在某个H地址下面的人口数量。
现在的问题是,数据集里面有5个H,结果跑完只给了4个,最后一个H a21sdf被忽略了。
我尝试在程序里
- else if type='P' then total+1
后多添加了一句
- if last.type then output;
可是结果依然取不出来。
日志显示last未初始化。使用last数据集需要先排序,是排序的话,数据集的顺序就打乱了。
我又试着把
- if last.type then output
改为 :
- if _n_=11 then output
(因为有11条记录)
结果显示为:
obs type address total
1 H zd2 0
2 H sdf 0
3 H a21a 1
4 H a21z 3
5 H a21sdf 0
这是我想要的结果。
但是不可能每次都去数一个数据集的记录条数是多少条。。
所以,请问各位大牛有没有什么方法解决这个问题?




雷达卡





京公网安备 11010802022788号







