Hi,
First of all, thank you for all your help.
I have two dates, I want to generate a new variable var=1 if the difference between two dates is less than 5 years.
date 1: 2015/01/02 00:02:25
date 2: 2014/04/21 09:21:36
Is it correct if I use the following condition:
0< date1 - date2 < 5 * 365.25 * 24 * 3600000
If not, which condition should I use? The date format is yyyy/mm/dd hh:mm:ss
Thank you
Freda
For your purpose, I would also add a method to the intck function:
var1 = intck("dtyear", date1, date2, 'C') >= 5;
In its default behaviour ('D' or 'Discrete'), the intck function would count the number of boundaries (00:00 on Jan 1st) over which the interval stretches, not the number of whole years (365.25 days) that have passed.
IE
data test;
input (date1 date2) (:yymmdd10.);
format date1 date2 yymmddd10.;
int1 = intck('year',date1,date2);
int2 = intck('year',date1,date2,'c');
cards;
2012-12-31 2017-01-01
2012-01-01 2017-12-31
;
run;
Do you have a datetime-variable or a char variable, that just looks like a datetime?
Assuming the first, you can use the function intck to calculate the difference:
var1 = intck("yeardt", date1, date2) >= 5;
I agree with @andreas_lds
how ever a slight change use 'dtyear' instead of 'yeardt'
var1 = intck("dtyear", date1, date2) >= 5;
For your purpose, I would also add a method to the intck function:
var1 = intck("dtyear", date1, date2, 'C') >= 5;
In its default behaviour ('D' or 'Discrete'), the intck function would count the number of boundaries (00:00 on Jan 1st) over which the interval stretches, not the number of whole years (365.25 days) that have passed.
IE
data test;
input (date1 date2) (:yymmdd10.);
format date1 date2 yymmddd10.;
int1 = intck('year',date1,date2);
int2 = intck('year',date1,date2,'c');
cards;
2012-12-31 2017-01-01
2012-01-01 2017-12-31
;
run;
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.