BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ChrisCarroll
Calcite | Level 5

Hi all,

I'm currently attempting to group observations where the days between observations is less than 10. The new column can be called group. I would like the title of the observations in a group to be the earliest date according to start_lactation. In the case of the data below, I would like observations 1-4 with the group column titled as 05/02/2011. Observations 5-7 should be left as blank. Observations 8 and 9 should be titled as 08/10/2011. Observations 10-15 should be titled as 25/01/2012. I need to do this for thousands of observations. I'm using sas 9.4.

Any help is greatly appreciated

 

data WORK.X;
infile datalines dsd truncover;
input TECHID:13. START_LACTATION:DDMMYY10. Daysapart:32.;
format TECHID 13. START_LACTATION DDMMYY10.;
label TECHID="TECHID";
datalines;
669222149 05/02/2011 .
669222145 05/02/2011 0
669222150 08/02/2011 3
669823432 10/02/2011 2
671847527 21/02/2011 11
669819685 17/03/2011 24
669819684 20/04/2011 34
669222144 08/10/2011 171
676229644 14/10/2011 6
748982347 25/01/2012 103
748982346 25/01/2012 0
744293316 26/01/2012 1
746731229 26/01/2012 0
748465662 27/01/2012 1
744813559 28/01/2012 1

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

@ChrisCarroll wrote:

Thanks that worked well. Is there a way of grouping the "group" column if there are less than 5 observations in any particular group and only if the group_start_date between the consecutive groups is less than 100 days. Thanks.


You could add a counter to track the number of observations per group? And then add another set of logic to analyze the data by groups to see if the 100 days between group starts. 

 

View solution in original post

3 REPLIES 3
Reeza
Super User
data want;
set x;
by techid start_lactation;*may need to presort your data;

retain group group_start_date;
if first.techid then do;
group=1; 
group_start_date = start_lactation;
end;
else if daysApart>10 then do;
  group+1; 
   group_start_date = start_lactation;
end;

run;

@ChrisCarroll wrote:

Hi all,

I'm currently attempting to group observations where the days between observations is less than 10. The new column can be called group. I would like the title of the observations in a group to be the earliest date according to start_lactation. In the case of the data below, I would like observations 1-4 with the group column titled as 05/02/2011. Observations 5-7 should be left as blank. Observations 8 and 9 should be titled as 08/10/2011. Observations 10-15 should be titled as 25/01/2012. I need to do this for thousands of observations. I'm using sas 9.4.

Any help is greatly appreciated

 

data WORK.X;
infile datalines dsd truncover;
input TECHID:13. START_LACTATION:DDMMYY10. Daysapart:32.;
format TECHID 13. START_LACTATION DDMMYY10.;
label TECHID="TECHID";
datalines;
669222149 05/02/2011 .
669222145 05/02/2011 0
669222150 08/02/2011 3
669823432 10/02/2011 2
671847527 21/02/2011 11
669819685 17/03/2011 24
669819684 20/04/2011 34
669222144 08/10/2011 171
676229644 14/10/2011 6
748982347 25/01/2012 103
748982346 25/01/2012 0
744293316 26/01/2012 1
746731229 26/01/2012 0
748465662 27/01/2012 1
744813559 28/01/2012 1


 

ChrisCarroll
Calcite | Level 5

Thanks that worked well. Is there a way of grouping the "group" column if there are less than 5 observations in any particular group and only if the group_start_date between the consecutive groups is less than 100 days. Thanks.

Reeza
Super User

@ChrisCarroll wrote:

Thanks that worked well. Is there a way of grouping the "group" column if there are less than 5 observations in any particular group and only if the group_start_date between the consecutive groups is less than 100 days. Thanks.


You could add a counter to track the number of observations per group? And then add another set of logic to analyze the data by groups to see if the 100 days between group starts. 

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 605 views
  • 0 likes
  • 2 in conversation