w********a 发帖数: 32 | 1
data A:
date y
2/1 0
2/2 1
2/3 1
2/4 0
2/5 0
2/6 1
2/7 0
2/8 1
2/9 1
2/10 1
;
自己琢磨了一阵子,不得窍门。需要知道y的value =1 每一次连续出现的次数 (最好
知道每次连续出现的起止日期)。由于实际数据很大, 不适合用Array.
final data will somehow look like:
date2 count
2/2 2
2/6 1
2/8 2
版上等, 谢谢大家!! |
k*******a 发帖数: 772 | 2 my code, date1 is start time date2 is stop time(different from your
date2)
data A;
input date $ y;
datalines;
2/1 0
2/2 1
2/3 1
2/4 0
2/5 0
2/6 1
2/7 0
2/8 1
2/9 1
2/10 1
;
run;
data b;
set a;
lagy=lag(y);
if y^=lagy then group+1;
run;
data b(keep=date1 date2 count);
retain date1 date2;
set b;
by group;
if y=1;
if first.group then do;
count=0;
date1=date;
end;
count+1;
if last.group then do;
date2=date;
output;
end;
run;
proc print data=b;run;
【在 w********a 的大作中提到】 : : data A: : date y : 2/1 0 : 2/2 1 : 2/3 1 : 2/4 0 : 2/5 0 : 2/6 1 : 2/7 0
|
r******m 发帖数: 369 | 3 data A;
input date $ y;
datalines;
2/1 0
2/2 1
2/3 1
2/4 0
2/5 0
2/6 1
2/7 0
2/8 1
2/9 1
2/10 1
;
/*create intermediate table that include rows you need*/
data middle (keep = startdate enddate count);
length startdate enddate $4;
retain startdate enddate count;
set a;
if y eq 0 then do;
count=0;
startdate=' ';
enddate=' ';
end;
if y eq 1 and count = 0 then do;
startdate = date;
enddate=date;
count + 1;
end;
else if y eq 1 and count >0 then do;
enddate = date;
count+1;
end;
run;
/*select rows you need*/
proc sql;
create table want as
select startdate,max(count)
from middle
where startdate is not null
group by startdate
order by startdate;
quit; |
l*********s 发帖数: 5409 | 4 data B(drop=count);
set A;
retain count;
by y notsorted;
if first.y then count=1;
else count+1;
if last.y then c=count;
if c=. then delete;
run; |
d******9 发帖数: 404 | 5 "by statement with unsorted option to identify the boundaries, and record
the starting date"
Great idea! Please give your full codes.
record
【在 l*********s 的大作中提到】 : data B(drop=count); : set A; : retain count; : by y notsorted; : if first.y then count=1; : else count+1; : if last.y then c=count; : if c=. then delete; : run;
|
k*******a 发帖数: 772 | 6 我也试过 by y,不过不行,原来要加 notsorted
学习了
【在 l*********s 的大作中提到】 : data B(drop=count); : set A; : retain count; : by y notsorted; : if first.y then count=1; : else count+1; : if last.y then c=count; : if c=. then delete; : run;
|
w********a 发帖数: 32 | 7 Appreciated all the help. :) |