Help using Base SAS procedures

Date Differences Calculation

Reply
New Contributor
Posts: 4

Date Differences Calculation


data myfile;

set file1;

tdy=&pbd_date;

ckdt=CK007_dt;

age=tdy-ckdt;

format age date9.;

run;

In our database &pbd_date is a numeric that can be converted to a date format.  ckdt is also a numeric

When I run the code, it initially produces numerics and a difference (ie 19663 - 19604 = 59)

If I format the age variable to a date9 I get wierd dates in the past (ie 19663 - 19604 = 29FEB1960)

If I format tdy ckdt age to date9 I get something like (01NOV2013-03SEP2013=29FEB1960)

I want the difference between 11/1/13 and 9/1/13 to be expressed in the actual date that is between the tdy and ck_dt.

Super User
Posts: 11,343

Re: Date Differences Calculation

When you subtract a date from another date you get the number of days between the values. Since SAS uses a base of 01Jan1960 for 'date' 0 then 59 days later is 29 Feb 1960.

If you need to get a count of specific intervals, such as months or weeks you want the INTCK function.

PROC Star
Posts: 7,468

Re: Date Differences Calculation

Not sure why you are calling your result "age" but, from what you described, does the following come close?:

%let pbd_date=19663;

data file1;

  informat ck007_dt date9.;

  input ck007_dt;

  cards;

03sep2013

;

data myfile;

  set file1;

  tdy=&pbd_date;

  ckdt=CK007_dt;

  age=tdy+((tdy-ckdt)/2);

  format age date9.;

run;

Contributor
Posts: 23

Re: Date Differences Calculation

I would use intck ...

data dates;
format d_now dob date9.;
d_now=today();
dob="1jan1970"d;
age_years=intck('years',dob,d_now);
run;

proc print data=dates; run;

This gives:

Obs      d_now                dob                age_years

1           05NOV2013      01JAN1970      43

Ask a Question
Discussion stats
  • 3 replies
  • 214 views
  • 0 likes
  • 4 in conversation