BookmarkSubscribeRSS Feed
Chitra
Calcite | Level 5

I need to find the difference between two date variables. one variable is in date9. format and other is in yymmddn8. format. I am not sure how to solve this problem. i tried to convert yymmddn8. format to date9. format and then use intck function to find the difference but not working. The other way i am trying is to convert both of them to SAS date values and simply subtract them, but doesn't seem to be as easy as i thought. I am unable to convert to SAS date values. can anyone help me regarding this.

Thanks.

3 REPLIES 3
Tom
Super User Tom
Super User

If they are date variables then you can just subtract them to find the difference in days.

data _null_;

  date1='23FEB2014'd ;

  date2='30MAR2014'd ;

  diff = date2 - date1 ;

  format date1 date9. date2 yymmddn8. ;

  put (_all_) (=);

run;

date1=23FEB2014 date2=20140330 diff=35


If they are instead character variables that look like they might be dates then you need to convert them to dates first.

data _null_;

  date1='23FEB2014';

  date2='20140330' ;

  diff = input(date2,yymmdd8.) - input(date1,date9.) ;

  put (_all_) (=);

run;


If one is really a date and the other is numeric variable that looks like a date (YYYYMMDD) then convert the later to a date first.

data _null_;

  date1='23FEB2014'd;

  date2=20140330 ;

  format date1 date9.;

  diff = input(put(date2,z8.),yymmdd8.) - date1 ;

  put (_all_) (=);

run;


Scott_Mitchell
Quartz | Level 8

If you have these dates in a external file of some description you can read them in a way similar to the below.

DATA HAVE;

INFILE DATALINES DLM=',';

INPUT D1:DATE9. D2:YYMMDD8.;

FORMAT D1 D2 DATE9.;

DATALINES;

01JAN2014,20140114

;

RUN;

DATA WANT;

SET HAVE;

  DATEDIFF = DATDIF(D1,D2,'ACT/ACT');

RUN;

ballardw
Super User

Apparently a partial misunderstanding of what a FORMAT is. FORMAT is a way to display a given value in a different form. It does not change the value of any variable.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 10303 views
  • 1 like
  • 4 in conversation