BookmarkSubscribeRSS Feed
hovliza
Obsidian | Level 7

Hello! I want to calculate the difference between two dates. The dates are displayed as YYYYMMDD and they are in format date9. I tried this;

DATA m.want;
SET m.have;
day=end-start;
RUN;

but it doesn't give the exact days between the dates. Is there another solution?

8 REPLIES 8
PeterClemmensen
Tourmaline | Level 20

"The dates are displayed as YYYYMMDD and they are in format date9. I tried this"... Please show us some of your data, this does not make much sense?

 

The INTCK Function is usually the way to go here..

hovliza
Obsidian | Level 7

Yes I tried this;

DATA m.want;
SET m.have;
month= intck('month', start, end);
RUN;

But that doesn't work..

hovliza
Obsidian | Level 7

It looks like this;

 

start               end

20100313      20141231

20101127      20160421

20091021      20160519

PeterClemmensen
Tourmaline | Level 20

If you want the number of months between start and end, your code is correct.

 

If you want the difference in days (which I assume), do like this

 

data have;
input (start end)(:yymmdd10.);
format start end date9.;
datalines;
20100313 20141231
20101127 20160421
20091021 20160519
;

data want;
	set have;
	daysbetween=intck('day', start, end);
run;
hovliza
Obsidian | Level 7

Thanks! For the three differences it works. I have 107 persons, is there an easier way to put at the 107 persons in the datalines?

Satish_Parida
Lapis Lazuli | Level 10

Data looks like this;

start               end

20100313      20141231

20101127      20160421

20091021      20160519

 

and you had tried 

month= intck('month', start, end);

So this can be assumed you need difference in months.

And the above did not work so both the variable start and end are not numeric or one of them not numeric.

 

1. Please identify the non-numeric type data first and change it to numeric data type using format yymmdd8.

    e.g.1. if start is charecter then do as following

    start1=input(start,yymmdd8.);

    e.g.1. if end is charecter then do as following

    end1=input(end,yymmdd8.);

2. Then try the intck function for the difference.

 

Please note I have used a different variable while changing datatype in the example.

 

Please let us know if it helped you.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Difference in terms of what?  Days, months, years?  For days a simple division is fine:

data want;
  a="01feb2018"d;
  b="16Mar2018"d;
  days=b-a;
  format a b yymmdd10.;
run;

For months and years intck is the better choice.  

Kurt_Bremser
Super User

@hovliza wrote:

Hello! I want to calculate the difference between two dates. The dates are displayed as YYYYMMDD and they are in format date9. I tried this;

DATA m.want;
SET m.have;
day=end-start;
RUN;

but it doesn't give the exact days between the dates. Is there another solution?


Maxim 3: Know your data. One of your variables is not a SAS date value. Look at the types and display formats of end and start in m.have.

Once you know your data, convert so both variables contain SAS date values.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 8 replies
  • 7017 views
  • 0 likes
  • 5 in conversation