DATA Step, Macro, Functions and more

Count IDs in any Block of 6 month period

Reply
New Contributor
Posts: 2

Count IDs in any Block of 6 month period

Please I need help....

      I have one years' worth of data, I want to flag any ID that occurs more than  1 in any 6-month period from the date on the row of that ID

 

Have

 

ID     Date

123   1/1/15

456   2/2/15

123   2/20/15

.         .

.         .

.         .

 

Want

 

ID       Count

123      2

.

.

.

 

 

Thank you!

Trusted Advisor
Posts: 1,128

Re: Count IDs in any Block of 6 month period

 

data have;
input ID     Date: mmddyy10.;
cards;
123   1/1/15
456   2/2/15
123   2/20/15
;

proc sort data=have;
by id date;
run;

data want;
set have;
by id date;
if first.id then count=1;
else count+1;
if last.id;
run;

 

Thanks,
Jag
Respected Advisor
Posts: 4,644

Re: Count IDs in any Block of 6 month period

Group by every observation (ID, date) in a proc SQL self join : 

 

proc sql;
select a.id, a.date, 1 + count(b.id) as count
from have as a inner join have as b on a.id=b.id and 
    b.date > a.date and intck("MONTH", a.date, b.date, "C") < 6
group by a.id, a.date;
quit;
PG
Super User
Posts: 9,676

Re: Count IDs in any Block of 6 month period

[ Edited ]

I don't understand what you mean.

You mean ID must have 2-6 month data ?

What if a ID have two or more obs in one month ?

data have;
input ID Date : mmddyy10.;
format date mmddyy10.;
cards;
123 1/1/15
456 2/2/15
123 2/20/15
;
run;
proc sql;
create table want as
 select id,count(*) as n
  from have
    group by id having count(distinct month(date)) between 2 and 6;
quit; 
Ask a Question
Discussion stats
  • 3 replies
  • 203 views
  • 0 likes
  • 4 in conversation