BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
freda
Fluorite | Level 6

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

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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;

View solution in original post

3 REPLIES 3
andreas_lds
Jade | Level 19

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;
Jagadishkatam
Amethyst | Level 16

I agree with @andreas_lds

 

how ever a slight change use 'dtyear' instead of 'yeardt'

 

 

var1 = intck("dtyear", date1, date2) >= 5;
Thanks,
Jag
Kurt_Bremser
Super User

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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 3 replies
  • 1032 views
  • 0 likes
  • 4 in conversation