BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Sujithpeta
Quartz | Level 8

Hello,

 

I've two variables DEATH_DT with format BEST12. and INDEX with format YYMMDDN8. I tried following to get dates difference.

 

data M.Death;
set M.Death;
format DEATH_Date YYMMDDN8.;
DEATH_Date = DEATH_DT;

death_days = (DEATH_Date-Index); run;

Surprisingly the outcome of death_days is also date with format BEST12. I highly appreciate if someone could help me with this.

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

@Sujithpeta wrote:
data WORK.DEATH;
  infile datalines dsd truncover;
  input DEATH_DT:32. Index:YYMMDDN8.;
  format Index YYMMDDN8.;
  label Index="Claim Admission Date";
datalines;
. 20120910
. 20120801
. 20120927
20140520 20120809
. 20121019
. 20120103
. 20120731
. 20120308
20150121 20120109
. 20121005
;;;;

Does this help @Kurt_Bremser?


Testing code before posting it here is not a crime, so please do it. You once again committed the mistake of using a non-existing informat that I already advised you about.

After fixing the data step, it can be clearly proven that @Reeza's code as initially posted works:

data WORK.DEATH;
input DEATH_DT :32. Index :YYMMDD8.;
format Index YYMMDDN8.;
label Index="Claim Admission Date";
datalines4;
. 20120910
. 20120801
. 20120927
20140520 20120809
. 20121019
. 20120103
. 20120731
. 20120308
20150121 20120109
. 20121005
;;;;
run;

data Death;
set Death;
format death_sas_date yymmddn8.;
death_sas_date = input(put(death_dt, 8. -l), yymmdd10.);
death_days = death_sas_date - Index;
run;

proc print data=death noobs;
run;

Result:

                         death_     death_
DEATH_DT       Index    sas_date     days

       .    20120910           .        . 
       .    20120801           .        . 
       .    20120927           .        . 
20140520    20120809    20140520      649 
       .    20121019           .        . 
       .    20120103           .        . 
       .    20120731           .        . 
       .    20120308           .        . 
20150121    20120109    20150121     1108 
       .    20121005           .        . 

View solution in original post

18 REPLIES 18
Reeza
Super User

@Sujithpeta wrote:

Hello,

 

I've two variables DEATH_DT with format BEST12. and INDEX with format YYMMDDN8. I tried following to get dates difference.

 


A 'date' with a best12 format will not be a SAS date and you cannot do a subtraction and expect the correct value, assuming it looks like YYMMDD, ie 20180101 as a number. You will first need to convert it to a date and then use it. However the exact code depends on how the variable looks like (the informal to use) and you would need to provide that information Your code would end up looking like:

 

death_sas_date = input(put(death_dt, 8. -l), yymmdd10.);

death_days = death_sas_date - Index;
PaigeMiller
Diamond | Level 26

@Reeza wrote:

@Sujithpeta wrote:

Hello,

 

I've two variables DEATH_DT with format BEST12. and INDEX with format YYMMDDN8. I tried following to get dates difference.

 


A 'date' with a best12 format will not be a SAS date

Sure it can be a SAS date. Formats don't change whether or not something is a SAS date.

--
Paige Miller
Reeza
Super User

@PaigeMiller wrote:

@Reeza wrote:

@Sujithpeta wrote:

Hello,

 

I've two variables DEATH_DT with format BEST12. and INDEX with format YYMMDDN8. I tried following to get dates difference.

 


A 'date' with a best12 format will not be a SAS date

Sure it can be a SAS date. Formats don't change whether or not something is a SAS date.


A 'date' with a best12 format will not be a SAS date and you cannot do a subtraction and expect the correct value, assuming it looks like YYMMDD, ie 20180101 as a number.

 

I stand by that statement 🙂 If it's a number and looks like YYMMDD (20180101) with the BEST format it's 99% unlikely to be a SAS date.

If it was 21048 that's likely a SAS date. 

PaigeMiller
Diamond | Level 26

@Reeza wrote:

@PaigeMiller wrote:

@Reeza wrote:

@Sujithpeta wrote:

Hello,

 

I've two variables DEATH_DT with format BEST12. and INDEX with format YYMMDDN8. I tried following to get dates difference.

 


A 'date' with a best12 format will not be a SAS date

Sure it can be a SAS date. Formats don't change whether or not something is a SAS date.


A 'date' with a best12 format will not be a SAS date and you cannot do a subtraction and expect the correct value, assuming it looks like YYMMDD, ie 20180101 as a number.

 

I stand by that statement 🙂 If it's a number and looks like YYMMDD (20180101) with the BEST format it's 99% unlikely to be a SAS date.

If it was 21048 that's likely a SAS date. 


Up until a later post, there was no such statement by the original poster that "it looks like YYMMDD", and so this (at the time) is an unsupported assumption.

--
Paige Miller
PaigeMiller
Diamond | Level 26

Can you show us the values on at least one observation of variables DEATH_DT and INDEX? Thank you.

--
Paige Miller
Sujithpeta
Quartz | Level 8

First column is the DEATH_DT with format BEST12. Length 8 and Informat 12.

Second column is the INDEX with format YYMMDDN8. Length 8 and informat 8.

 

I don't know why the date is in a non-SAS format because it's the original datasetsDeath.PNG

Reeza
Super User

Given this, the code I posted earlier should work. If it does not, post your code and log. 

 


@Sujithpeta wrote:

First column is the DEATH_DT with format BEST12. Length 8 and Informat 12.

Second column is the INDEX with format YYMMDDN8. Length 8 and informat 8.

 

I don't know why the date is in a non-SAS format because it's the original datasetsDeath.PNG


 

Sujithpeta
Quartz | Level 8

@Reeza I tried the code but it's throwing error.

 

Death.PNG

 

I also tried changing the YYMMDDN10. to YYMMDDN8. I had the same error.

Sujithpeta
Quartz | Level 8

My bad @Kurt_Bremser, I corrected the code as @Reeza posted 

 

 

data M.Death;
set M.Death;
  Death_SAS_Date = input(put(DEATH_DT, 8. -l), YYMMDD10.) - Index;
  Death_days = Death_SAS_Date - Index;
run;

 

Death_SAS_Date still in the format of BEST12. and Death_days are also in the format of BEST12.

 

Death.PNG

Sujithpeta
Quartz | Level 8
data WORK.DEATH;
  infile datalines dsd truncover;
  input DEATH_DT:32. Index:YYMMDDN8.;
  format Index YYMMDDN8.;
  label Index="Claim Admission Date";
datalines;
. 20120910
. 20120801
. 20120927
20140520 20120809
. 20121019
. 20120103
. 20120731
. 20120308
20150121 20120109
. 20121005
;;;;

Does this help @Kurt_Bremser?

Kurt_Bremser
Super User

@Sujithpeta wrote:
data WORK.DEATH;
  infile datalines dsd truncover;
  input DEATH_DT:32. Index:YYMMDDN8.;
  format Index YYMMDDN8.;
  label Index="Claim Admission Date";
datalines;
. 20120910
. 20120801
. 20120927
20140520 20120809
. 20121019
. 20120103
. 20120731
. 20120308
20150121 20120109
. 20121005
;;;;

Does this help @Kurt_Bremser?


Testing code before posting it here is not a crime, so please do it. You once again committed the mistake of using a non-existing informat that I already advised you about.

After fixing the data step, it can be clearly proven that @Reeza's code as initially posted works:

data WORK.DEATH;
input DEATH_DT :32. Index :YYMMDD8.;
format Index YYMMDDN8.;
label Index="Claim Admission Date";
datalines4;
. 20120910
. 20120801
. 20120927
20140520 20120809
. 20121019
. 20120103
. 20120731
. 20120308
20150121 20120109
. 20121005
;;;;
run;

data Death;
set Death;
format death_sas_date yymmddn8.;
death_sas_date = input(put(death_dt, 8. -l), yymmdd10.);
death_days = death_sas_date - Index;
run;

proc print data=death noobs;
run;

Result:

                         death_     death_
DEATH_DT       Index    sas_date     days

       .    20120910           .        . 
       .    20120801           .        . 
       .    20120927           .        . 
20140520    20120809    20140520      649 
       .    20121019           .        . 
       .    20120103           .        . 
       .    20120731           .        . 
       .    20120308           .        . 
20150121    20120109    20150121     1108 
       .    20121005           .        . 
Sujithpeta
Quartz | Level 8

@Reeza @Kurt_Bremser thanks guys, I apologize for not properly following the instructions.  

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 18 replies
  • 2511 views
  • 1 like
  • 4 in conversation