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

Hello good morning, I could not find the way to do the following, I have an ID and a date of a certain event. I must add the difference in months of an event with the following one by ID.

Example in excel in the photograph.

someone who can help me, Greetings.

Andres_Fuentes1_0-1624382354559.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
Jerrya00
Fluorite | Level 6
Please try this:
proc sort data=your_data; by ID date;
run;

data results;
set your_data;
by ID date;;
retain pre_Date .;
if first.ID then do;
n_month=.;
pre_Date=date;
end;
else do;
n_month=intck('month', pre_Date, date, 'C');
pre_Date=date;
end;
drop pre_Date;
run;

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

And so what is the desired result? Do you want 9 for ID = 1 and 12 for ID = 2 and 12 for ID=3?

 

Is your date variable a character variable in SAS or a numeric variable in SAS? What does PROC CONTENTS say?

 

FYI, please do not provide data as screen captures from Excel. The preferred method to provide data is: 

https://blogs.sas.com/content/sastraining/2016/03/11/jedi-sas-tricks-data-to-data-step-macro/

 

--
Paige Miller
Reeza
Super User

That's Excel, not SAS data shown. In the future please show the SAS version. I'm assuming you have your data imported and that your date are SAS dates, eg numeric with a date format.

 

proc sort data=have;
by ID;
run;

data want;
set have;

by ID;

prev_date = lag(date);

if not first.id then n_month = intck('month', date, prev_date, 'C');

run;

You may need to play with the INTCK() function to get the exact numbers you want but this gives you an idea of how to do it. 

 


@Andres_Fuentes1 wrote:

Hello good morning, I could not find the way to do the following, I have an ID and a date of a certain event. I must add the difference in months of an event with the following one by ID.

Example in excel in the photograph.

someone who can help me, Greetings.

Andres_Fuentes1_0-1624382354559.png

 


 

Jerrya00
Fluorite | Level 6
Please try this:
proc sort data=your_data; by ID date;
run;

data results;
set your_data;
by ID date;;
retain pre_Date .;
if first.ID then do;
n_month=.;
pre_Date=date;
end;
else do;
n_month=intck('month', pre_Date, date, 'C');
pre_Date=date;
end;
drop pre_Date;
run;
Andres_Fuentes1
Calcite | Level 5

Nice, Thanks you.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 586 views
  • 0 likes
  • 4 in conversation