DATA Step, Macro, Functions and more

How to find the difference between two date variables which are in two different formats.

Reply
N/A
Posts: 1

How to find the difference between two date variables which are in two different formats.

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.

Super User
Super User
Posts: 7,076

Re: How to find the difference between two date variables which are in two different formats.

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;


Super Contributor
Posts: 297

Re: How to find the difference between two date variables which are in two different formats.

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 D1Smiley Very HappyATE9. D2:YYMMDD8.;

FORMAT D1 D2 DATE9.;

DATALINES;

01JAN2014,20140114

;

RUN;

DATA WANT;

SET HAVE;

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

RUN;

Super User
Posts: 11,343

Re: How to find the difference between two date variables which are in two different formats.

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.

Ask a Question
Discussion stats
  • 3 replies
  • 5237 views
  • 1 like
  • 4 in conversation