Calcite | Level 5

## Diference Date (MONTH) by ID

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Fluorite | Level 6

## Re: Diference Date (MONTH) by ID

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;
4 REPLIES 4
Diamond | Level 26

## Re: Diference Date (MONTH) by ID

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
Super User

## Re: Diference Date (MONTH) by ID

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.

Fluorite | Level 6

## Re: Diference Date (MONTH) by ID

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;
Calcite | Level 5

## Re: Diference Date (MONTH) by ID

Nice, Thanks you.

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