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

From the sample data below, I'm trying to count how many consecutive months a member has. In the event if an ID has a gap month in the middle, the output should show the most recent count of consecutive months this member had, see example below.

dataset=one

 

ID Month
7201SEP2020
7201OCT2020
7201NOV2020
7201DEC2020
7201FEB2021
7201MAR2021
7201APR2021
7201MAY2021
7201JUN2021

desired output is as follows:

ID months_ct
725
1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

If the data are sorted by id/month and month is stored as a sas date value, then:

 

data want;
  set have;
  by id;
  if first.id=1 or intck('month',lag(month),month)^=1 then consec_months=1;
  else consec_months+1;
  if last.id;
run;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

5 REPLIES 5
mkeintz
PROC Star

If the data are sorted by id/month and month is stored as a sas date value, then:

 

data want;
  set have;
  by id;
  if first.id=1 or intck('month',lag(month),month)^=1 then consec_months=1;
  else consec_months+1;
  if last.id;
run;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
yunnieling
Calcite | Level 5
thanks, the result shows each member and most recent month, but not the total count of consecutive months.
mkeintz
PROC Star

Please show the log when describing unexpected results.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
yunnieling
Calcite | Level 5
Log:26
27 data want;
28 set idk;
29 by eci_id ;
30 if first.eci_id=1 or intck('month',lag(inc_mth_dt),month)^=1 then consec_months=1;
31 else consec_months+1;
32 if last.eci_id;
33 run;

NOTE: Variable month is uninitialized.
NOTE: Missing values were generated as a result of performing an operation on missing values.
Each place is given by: (Number of times) at (Line):(Column).
4162545 at 30:24
NOTE: There were 4162545 observations read from the data set WORK.IDK.
NOTE: The data set WORK.WANT has 65205 observations and 4 variables.
NOTE: DATA statement used (Total process time):
real time 0.35 seconds
cpu time 0.35 seconds


Below is the result.
ECI_ID INC_MTH_DT month consec_months
72 01SEP2021 . 1
125 01SEP2021 . 1
203 01SEP2021 . 1
325 01SEP2021 . 1
329 01SEP2021 . 1
371 01OCT2020 . 1
438 01SEP2021 . 1
466 01MAY2019 . 1
mkeintz
PROC Star

Please read the documentation on the INTCK and LAG functions, then complete your modification of the

 

           intck('month',lag(inc_mth_dt),month)

expression.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 2657 views
  • 0 likes
  • 2 in conversation