I need help in calculating the day difference between the following date formats
MyDATE | Char | 10 | $10. | $10. | MyDATE | |
APPDATE | Num | 8 | DATETIME20. | DATETIME20. | APPDATE |
@USHAKS wrote:
here how it looks
date time 16JUL2018:00:00:00
and character field 2018-07-19
You can use the YYMMDD informat to convert strings like '2018-07-19' to a date. You can use teh DATEPART() function to convert number of seconds like '16JUL2018:00:00:00'dt to a date. Then you can just subtract the two numbers.
diff = datepart(datetime_var) - input(char_var,yymmdd10.);
I'm going to assume you want it to remain numeric but want the format only changed.
data have;
attrib date1 informat=datetime20. format=datetime20.;
attrib date2 informat=datetime20. format=datetime20.;
input date1 :datetime20. date2 :datetime20.;
datalines;
16JUL2018:00:00:00 19JUL2019:00:00:00
;
run;
data want;
set have;
format formatted1 yymmdd10.;
format formatted2 yymmdd10.;
formatted1 = datepart(date1);
formatted2 = datepart(date2);
datedif = intck('days', datepart(date1), datepart(date2));
run;
If for whatever reason you are wanting the YYYYMMDD format to be character you can do this:
character1 = put(datepart(date1), yymmdd10.);
There might be a matter of semantics to ascertain before we can answer:
Do you currently have a numeric variable that contains a datetime?
Do you want to create a character variable that contains a string, or do you want a numeric variable containing a SAS date*?
Asking that since it is odd that you'd want to find the date difference between 2 strings.
*A third option is keeping the datetime value and displaying just the date part
I have a date two date fields
I need help in calculating the day difference between the following date formats
MyDATE | Char | 10 | $10. | $10. | MyDATE | |
APPDATE | Num | 8 | DATETIME20. | DATETIME20. | APPDATE |
date time 16JUL2018:00:00:00
and character format as per proc contents procedure 2018-07-19
@USHAKS wrote:
I have a date two date fields
one is in date time format
another date is in character format (as shown below)
date time 16JUL2018:00:00:00
and character field 2018-07-19
So i want the day difference between these two dates which are in different format
Formats are not relevant to this problem. Also, it doesn't help to tell us that this is a character format -- the key information we need is if the variable (not the format) is numeric or character, this can be found from PROC CONTENTS and other methods.
Assuming it is a numeric variable, to find the difference in days between a datetime variable and a date variable, use something like this:
difference = datepart (datetimevariable) - datevariable;
So when you right-click on the columns in question and select Column Attributes they're both showing as Character Type and not Numeric?
I'm guessing they're both Numeric, the format's are just different for each. One being a datetime and the other a date.
@USHAKS wrote:
I need help in converting the date time 16JUL2018:00:00:00 to character format 2018-07-16
and then find the day difference between 2018-07-16 and 2018-07-19
First, date/times should remain numeric. Converting them to character does not help at all in finding differences in days, and you can always display your numeric date/time with a format and so people who are looking at it will see 2018-07-16. In a DATA step:
datevar=datepart(datetimevariable);
format datevar yymmddd10.;
The difference between days, once you take the datepart of your date/time variable, is simply a subtraction.
I need help in calculating the day difference between the following date formats
MyDATE | Char | 10 | $10. | $10. | MyDATE | |
APPDATE | Num | 8 | DATETIME20. | DATETIME20. | APPDATE |
@USHAKS wrote:
I need help in calculating the day difference between the following date formats
MyDATE Char 10 $10. $10. MyDATE APPDATE Num 8 DATETIME20. DATETIME20. APPDATE
It looks like you have one character variable and one numeric variable. Actually, this is where the conversation over how to do things with dates or datetimes should start, by showing us the PROC CONTENTS so we can see if the variables are numeric or text, rather than just showing us the variable values. The formats are largely irrelevant to determining the difference between the dates.
But now we need to see how MyDATE appears, as we need to know that, and it seems as if you have edited out that information.
here how it looks
date time 16JUL2018:00:00:00
and character field 2018-07-19
@USHAKS wrote:
here how it looks
date time 16JUL2018:00:00:00
and character field 2018-07-19
You can use the YYMMDD informat to convert strings like '2018-07-19' to a date. You can use teh DATEPART() function to convert number of seconds like '16JUL2018:00:00:00'dt to a date. Then you can just subtract the two numbers.
diff = datepart(datetime_var) - input(char_var,yymmdd10.);
@USHAKS wrote:
here how it looks
date time 16JUL2018:00:00:00
and character field 2018-07-19
Okay, now I think we are ready to go! We will use INPUT() to turn the character variable MYDATE into a numeric SAS date value, and we will use datepart to turn the numeric datetime value into a numeric date value. Once they are both numeric SAS date values, the two can be subtracted to determine the difference.
data want;
set have;
difference = input(mydate,anydtdte.) - datepart(appdate);
run;
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.