DATA Step, Macro, Functions and more

Difference between two dates YYYYMMDD format date9.

Reply
Contributor
Posts: 32

Difference between two dates YYYYMMDD format date9.

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?

PROC Star
Posts: 1,218

Re: Difference between two dates YYYYMMDD format date9.

"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..

Contributor
Posts: 32

Re: Difference between two dates YYYYMMDD format date9.

Yes I tried this;

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

But that doesn't work..

Contributor
Posts: 32

Re: Difference between two dates YYYYMMDD format date9.

It looks like this;

 

start               end

20100313      20141231

20101127      20160421

20091021      20160519

PROC Star
Posts: 1,218

Re: Difference between two dates YYYYMMDD format date9.

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;
Contributor
Posts: 32

Re: Difference between two dates YYYYMMDD format date9.

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?

Frequent Contributor
Posts: 109

Re: Difference between two dates YYYYMMDD format date9.

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.

Super User
Super User
Posts: 9,441

Re: Difference between two dates YYYYMMDD format date9.

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.  

Super User
Posts: 9,923

Re: Difference between two dates YYYYMMDD format date9.


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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Ask a Question
Discussion stats
  • 8 replies
  • 345 views
  • 0 likes
  • 5 in conversation