BookmarkSubscribeRSS Feed
Olaade
Calcite | Level 5

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!

3 REPLIES 3
Jagadishkatam
Amethyst | Level 16

 

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
PGStats
Opal | Level 21

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
Ksharp
Super User

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; 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 882 views
  • 0 likes
  • 4 in conversation