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

I need help in calculating the day difference between the following date formats

 

MyDATEChar10$10.$10.MyDATE 
APPDATENum8DATETIME20.DATETIME20.APPDATE 
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@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.);

View solution in original post

12 REPLIES 12
Krueger
Pyrite | Level 9

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.);
ChrisNZ
Tourmaline | Level 20

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

USHAKS
Fluorite | Level 6

I have a date two date fields

I need help in calculating the day difference between the following date formats

 

MyDATEChar10$10.$10.MyDATE 
APPDATENum8DATETIME20.DATETIME20.APPDATE 

date time 16JUL2018:00:00:00  

 

 and character format as per proc contents procedure 2018-07-19

 

PaigeMiller
Diamond | Level 26

@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;

 

--
Paige Miller
Krueger
Pyrite | Level 9

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.

PaigeMiller
Diamond | Level 26

@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.

--
Paige Miller
USHAKS
Fluorite | Level 6

I need help in calculating the day difference between the following date formats

 

MyDATEChar10$10.$10.MyDATE 
APPDATENum8DATETIME20.DATETIME20.APPDATE 
PaigeMiller
Diamond | Level 26

@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.

--
Paige Miller
USHAKS
Fluorite | Level 6

here how it looks

date time 16JUL2018:00:00:00  

 

 and character field  2018-07-19

Tom
Super User Tom
Super User

@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.);
PaigeMiller
Diamond | Level 26

@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;
--
Paige Miller
USHAKS
Fluorite | Level 6
Thank you that helped.

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
  • 12 replies
  • 3036 views
  • 3 likes
  • 5 in conversation