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

Hello,

I'm a beginner of SAS. Could anyone help me out? Thank you!

Here is a sample dataset. I have data with patientID, year and month.

PatientID  Year Month
K1234T     2012  1
K1234T     2012  2
K1234T     2012  3
K1234T     2012  8
M2350Y    2012  7
I4656Y     2012   5

I4656Y     2012   10

The final dataset I need is, in the final dataset, if a patient has consecutive month, then
just keep one record and create earlist month and latest month columns (e.g. for K1234T,min_Month=1 ,max_Month=3).If not consecutive month, min_Month=max_Month=Month for this record.

So the final dataset would like:
PatientID  Year min_Month max_Month
K1234T     2012  1          3
K1234T     2012  8          8
M2350Y     2012  7          7
I4656Y     2012  5          5

I4656Y     2012  10       10

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15

This works for your presented data:

data have;

input patientid :$8. year month;

cards;

K1234T     2012  1

K1234T     2012  2

K1234T     2012  3

K1234T     2012  8

M2350Y    2012  7

I4656Y     2012   5

I4656Y     2012   10

;

data want;

do until (last.patientid);

  set have;

    by patientid notsorted;

if first.patientid then do; min=month; max=month; _mon=month;end;

else if month-_mon=1 then do; max=month; _mon=month;end;

else if month-_mon>1 then do; output; min=month; max=month; _mon=month;end;

if last.patientid then output;

end;

drop month _:;

run;

However, if you have more than one year of data, then you need

1) Concatenated year and month to make a SAS date variable

2) Use intck() to get the difference.

Haikuo

View solution in original post

4 REPLIES 4
ballardw
Super User

What would the desired output be for:

K1234T     2012  12

K1234T     2013  1

Consecutive months across the year boundary?

deleted_user
Not applicable

My data only have  year 2012, no other year.

Thank you!

Haikuo
Onyx | Level 15

This works for your presented data:

data have;

input patientid :$8. year month;

cards;

K1234T     2012  1

K1234T     2012  2

K1234T     2012  3

K1234T     2012  8

M2350Y    2012  7

I4656Y     2012   5

I4656Y     2012   10

;

data want;

do until (last.patientid);

  set have;

    by patientid notsorted;

if first.patientid then do; min=month; max=month; _mon=month;end;

else if month-_mon=1 then do; max=month; _mon=month;end;

else if month-_mon>1 then do; output; min=month; max=month; _mon=month;end;

if last.patientid then output;

end;

drop month _:;

run;

However, if you have more than one year of data, then you need

1) Concatenated year and month to make a SAS date variable

2) Use intck() to get the difference.

Haikuo

deleted_user
Not applicable

Haikuo, thank you very much!

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 4 replies
  • 2588 views
  • 0 likes
  • 3 in conversation