Converting Date column to proper sas dates and creating Year/month columns, from this date 11FEB13:22:59:00 creating year column

Viewed 421

I have a Date column, Type - Numeric, Format - DATETIME., Informat - ANYDTDTM40,

data looks like this:

Date
11FEB13:22:59:00
29NOV13:18:03:00

Out of this date column i need to create year and month columns.

However when i try:

DATA Final;
Set Final;
Year = Year(Date);
Run;

I receive null values in the year column.

2 Answers

Presume date variable contains DateTime values:

year_int = YEAR(DATEPART(date));

SAS Date and DateTime values are numerics with different interpretations. A Date value is number of days from epoch and DateTime is number of seconds.

There are different functions for extracting different aspects of theses values:

DateTime examples

  • YearPart(<datetime-value>)
  • TimePart(<datetime-value>)
  • DateTime() - current system datetime

Date examples

  • Year(date-value)
  • Month(<date-value>)
  • Day(<date-value>)

One of the best organized references for these classes of functions is found in the ETS documentation which documents a whopping 36 different functions.

The DS2 version of this documentation is pretty clean too.

Base SAS documentation lists the same Date and Time functions in page "SAS Functions and CALL Routines by Category"

Try using format!

 set Final;
format date date9.;
Run;
DATA Final;
Set Final;
Year = year(date);
Run;
Related