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

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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