SAS创建重置的计数变量

时间:2016-05-10 14:16:08

标签: sas

我想创建一个按犯罪方式每6个月重置一次的计数变量。这是我的代码:

proc sort data=incident_v1;
by incident year;
run;

Data incident_v2;
set incident_v1 end= eof;
by incident;
do i=1 until (eof);

do j = 1 to 6;
          retain ID 0;
if first.incident then  ID=ID +1;
end;

         end;
                    run;

年份事件#事件数量每六个月计算一次 199901 car-jack 6 1 199902 car-jack 7 2 199903 car-jack 12 3 199904 car-jack 8 4 199905 car-jack 13 5 199906 car-jack 8 6 199907 car-jack 13 1 199908 car-jack 6 2 199909 car-jack 8 6 200001抢劫5 1 200002抢劫5 2 200003抢劫8 3 200004抢劫4 4 200005抢劫6 5 200006抢劫14 6

1 个答案:

答案 0 :(得分:0)

试试这个:

proc sort data=incident_v1;
by incident year;
run;

Data incident_v2;
 set incident_v1 end= eof;
 by incident;
 /*Reset Count if New Incident Type*/
 if first.incident then Incident_count_6_mnths = 0;
 /*Reset Count if month is 07 (which from your data below I believe to be actually year & month and of the form YYYYMM*/
 if substr(year,5,2) = "07" then Incident_count_6_mnths = 0; 
 /*if year is a not a text field you will have to use: substr(compress(put(year,8.)),5,2) = "07" */

 /*Add a counter - please note there is no need for a retain statement as the +1 will automatically retain.*/
 Incident_count_6_mnths+1;
run;