SAS-date-format-valtitude

30 May 2009

Converting Date formats in SAS

How to Change OR Converting Date Format in SAS

SAS is very helpful but can be complex as well. Because of the flexibility it offers, it has to be necessarily complex. The manuals from the Institute are not very comprehensive to cover the length and breadth of the complexity.

How to converts dates and times into the combination format date time? Extracting dates and times out of a date time format is easy. We can use the function date part() and time part() on the date time value and write them as specific formats in the output dataset.

Here is the code to extract date and time out of a combination date time value:

data base;
format date date9. time time11.2;
set allbase;

date=datepart(billing_day); time=timepart(billing_day);

The second question is if you have dates and times, how do you covert this to a datetime value? SAS does not provide much of an answer here in its manuals. Not much online help is available either. Let us say the problem is to convert the Date Value in a date9. format into a datetime20. format with time being midnight of that day meaning 00:00.

The approach is to take the following steps:
1. convert the SAS date value into a character string using the PUT function
2. concatenate it with the string of the time values and
3. convert it back to datetime using the INPUT function
4. Use the format statement with the datestep to let SAS know that you will display with datetime20. format.

Here is the code of Converting Date Formats in SAS:

data base;
format datex date9. datey $9. datez datetime20.;

datex = intnx('month','05jan2002’d, +1);
datey = put(datex,date9.);* convert the date to a character string;

datez = input(datey||":00:00:00",datetime20.); * this input function reads a character value;

* the put function displays the SAS date into a readable date, but remember the date is still retained as a date value;

put datez / datez datetime20.;
output;

Note that the put function converts the input into a character value. The input function uses a character or string value and converts it into the SAS date value requested. SAS date is a number that can be displayed using the appropriate formats. That is done by the format statement in the data step.

These are complex functions but very powerful and can be put to good use in data base development and moving data across different operating systems.

Leave a Reply