turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- BI
- /
- Enterprise Guide
- /
- Date conversion to integer?

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-30-2014 04:06 PM

Hi -

I have a proc sgl statement in an EG program that includes two dates:

select | datepart (mcl.ServiceDateFrom) as IncurredDt format mmddyy10. |

I need to convert that from a SAS date into a number formatted as YYYYMMDD but I cant figure out how.

Can anyone please help?

Thanks!!

John

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-30-2014 04:14 PM

Here's an oldie:

I haven't checked it so you may need to tweak the number of zeroes in a particular location.

Year(incurred_dt)*10000+month(incurred_dt)*100+day(incurred_dt)

Also, I generally don't recommend doing this because its then more difficult to do date calculations such as durations.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-30-2014 04:15 PM

Thank you, Reeza.

I agree with you but my boss wants it that way!!!

John

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-30-2014 04:21 PM

Use PUT() and INPUT().

select

input(put(datepart(mcl.ServiceDateFrom),yymmddn8.),8.) as IncurredDt format=8.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-31-2014 08:56 AM

Wow, Tom. Thank you for that, that looks complicated! I an new to SAS but love it so far.........

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-31-2014 08:58 AM

Another question, Tom and Patrick.....

Does using the datepart() function mean that the date is still formatted like a date or is it a number?

Thanks guys, I really appreciate the help!!

John

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-31-2014 09:35 AM

SAS has only two data types. Floating Point numbers and fixed length character strings.

To implement date, time and datetime values SAS just defined a convention for what the numbers mean and build formats, informats and functions that can deal with those floating point numbers and interpret them properly.

For dates SAS stores the number of days since 1/1/1960.

For time SAS stores the number of seconds since midnight.

For datetime SAS stores the number of seconds since 1/1/1960.

To convert from datetime to date you really just need to divide by one days worth of seconds (24*60*60) and take the integer part. That is what the DATEPART() function does for you.

Formats are SAS's method for controlling how to display the values. There are various formats related to date, time and datetime.

Informats are SAS's method for converting displayed values into the corresponding stored value.

So in the program I used DATEPART() to convert your datetime value into just a date and use the PUT() function to apply the YYMMDDn8. format to display the value as an 8 digit string. I then used the INPUT() function to read that 8 digit string as a regular number.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-31-2014 09:39 AM

That is a great explanation, Tom. Thank you very much.

John

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-31-2014 05:33 PM

And the only bit different to what Tom did was me using format b8601dn8. which returns the same string than YYMMDDn8. but take as SAS datetime value as input so the conversion from a SAS datetime value to a SAS date value using "datepart()" is no more necessary.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-30-2014 09:01 PM

Same as Tom's but using a format which returns the "yyyymmdd" string directly from a datetime value without the need to use the datepart() function.

select

input(put(mcl.ServiceDateFrom,b8601dn8.),8.) as IncurredDt format=8.