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?
"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..
Yes I tried this;
DATA m.want;
SET m.have;
month= intck('month', start, end);
RUN;
But that doesn't work..
It looks like this;
start end
20100313 20141231
20101127 20160421
20091021 20160519
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;
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?
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.
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.
@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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.