- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
"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..
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Yes I tried this;
DATA m.want;
SET m.have;
month= intck('month', start, end);
RUN;
But that doesn't work..
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
It looks like this;
start end
20100313 20141231
20101127 20160421
20091021 20160519
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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.