DATA Step, Macro, Functions and more

Date difference

Reply
Contributor
Posts: 58

Date difference

[ Edited ]

Hey guys;

 

I'm trying to get days difference between two dates in the format YYMMDDN8. 

data M.Master;
set M.Master;
by DESY_SORT_KEY;
Day_Death = intck('dtday', Index, DEATH_DT);
run;

Every row has same days, Can you guys help me out.

 

Thanks!

Super User
Posts: 6,637

Re: Date difference

Posted in reply to Sujithpeta

If you have two dates, you don't need INTCK:

 

Day_Death = Death_DT - Index;

Contributor
Posts: 58

Re: Date difference

Posted in reply to Astounding

I tried it, but I'm ending up getting a date format output instead of numeric days.

Super User
Posts: 6,637

Re: Date difference

Posted in reply to Sujithpeta

Not possible.  Give one example of the inputs and outputs that you believe to be incorrect.

Super User
Posts: 13,338

Re: Date difference

Posted in reply to Astounding

Astounding wrote:

Not possible.  Give one example of the inputs and outputs that you believe to be incorrect.


Perhaps the data set already has a variable Day_Death with a date format from a previous trial of this code exercise.

 

The

Data M.Master;

   set M.Master;

coding could well have added lots of things in prior runs such as FORMAT for Day_death.

Also use of 'dtday' also implies that you should have DATETIME variables, not Dates. So try 'day' instead or the subtraction.

.

You may want to go back and rebuild the M.Master set to before you attempted to add this variable the first time.

 

Contributor
Posts: 58

Re: Date difference

Posted in reply to Astounding
data M.Master;
set M.Master;
by DESY_SORT_KEY;
format DEATH_DT YYMMDDN8.;
format Index YYMMDDN8.;
if DEATH_DT NE "" then Death = 1;
Day_Death = (DEATH_DT - Index);
run;

Output:

Capture.PNG

Super User
Posts: 6,637

Re: Date difference

Posted in reply to Sujithpeta

Looking at these results, I would guess @ballardw hit the nail on the head.  Your existing data set already contains a variable named DAY_Death, and it already has a date format.  One easy way to get rid of that would be on the SET statement:

 

set M.Master (drop=Day_Death);

 

That assumes you have inspected the data and agree that you want to get rid of it.

Super User
Posts: 13,338

Re: Date difference

Posted in reply to Sujithpeta

It would also be interesting to see what the actual assigned format for the DAY_death variable is. In a value like 20101717 while 2010 might be a year the 1717 is not a valid month and day combination in any form, there is no month 17 in SAS dates.

 

And without a shown value for INDEX I'm at loss for what is happening.

Contributor
Posts: 58

Re: Date difference

Day_Death is created to calculate the days difference between days and don't exist before.

Index date originally was in the format YYMMDDN8. and DEATH_DT was in the format BEST12.

Super User
Posts: 13,338

Re: Date difference

Posted in reply to Sujithpeta

The internal numeric representation of a date corresponding to your Death_dt of 20130420 (20APR2013) is 19468. To have a value of

death_dt - index = 20111309 as shown above then Index would have to have a value of -20091841 which has a value prior to the earliest valid SAS date values (by about 19700000 days). HOWEVER that value of Index would correspond to a datetime of 13MAY59:10:55:59.

If index is supposed to be 20120428 (28APR2012) then your value of Death_dt is 20130420 and is not a SAS date variable at all.

 

So what is the actual value of your Index variable? and if the SAS format assigned is actually YYMMDDN8. then show the unformatted values for Death_dt and Index for a few records.

Proc print data=have;

   var death_dt index;

   format death_dt index  best12.;

run;

for example.

I believe from what you have shown that at least one of your variables does not actually have the SAS format you

Ask a Question
Discussion stats
  • 9 replies
  • 210 views
  • 0 likes
  • 3 in conversation