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

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.

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 328 views
  • 0 likes
  • 4 in conversation