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
@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 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;
@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.
@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.
@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.
Can you show us the values on at least one observation of variables DEATH_DT and INDEX? Thank you.
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 datasets
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 datasets
@Reeza I tried the code but it's throwing error.
I also tried changing the YYMMDDN10. to YYMMDDN8. I had the same error.
Read @Reeza's post thoroughly, with regards to the informat. There is no yymmddn informat, it only exists as format. Use the yymmdd informat.
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.
Post the dataset in a data step with datalines.
Until we can know your data (see Maxim 3), further guessing around is useless.
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?
@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 . .
@Reeza @Kurt_Bremser thanks guys, I apologize for not properly following the instructions.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: