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!

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!

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.

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