Help using Base SAS procedures

Extracting month/day from dates

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 9
Accepted Solution

Extracting month/day from dates


I have a dataset with dates in one column. I have been using functions in Excel to extract the year and the month/day as two separate columns from the full date prior to importing the dataset into SAS, so the dataset looks like this once imported into SAS:

Full_Date               Year          Date        Other Information

01JAN2012           2012           01/01       XYZ

03JUL2014           2014           07/03       ABC

26FEB2015          2015           02/26        DEF

We would like to instead be able to extract the year and date information and create these variables from the full_date directly in SAS after importing the file containing just the full date (and other info), without needing to first do the manipulation in Excel. So we would like to import the following, and make it look like the above dataset, all in SAS:

Full_Date             Other Information

01JAN2012           XYZ

03JUL2014           ABC

26FEB2015          DEF

I know SAS has a YEAR function, but does a function exist to extract month/date in this fashion too? Thank you for any help.


Accepted Solutions
Solution
‎03-17-2015 05:44 PM
Super User
Posts: 10,500

Re: Extracting month/day from dates

If the variable comes into SAS as a date value (numeric with a format like date9.) then its as easy as:

year=year(full_date);

date = catx('/',put(month(full_date),z2.),put(day(full_date),z2.));

Date pretty much must be text as you're showing here.

You could also create a custom format to do this directly, Look up Proc Format and date predicates as an exercise for the interested reader.

View solution in original post


All Replies
Solution
‎03-17-2015 05:44 PM
Super User
Posts: 10,500

Re: Extracting month/day from dates

If the variable comes into SAS as a date value (numeric with a format like date9.) then its as easy as:

year=year(full_date);

date = catx('/',put(month(full_date),z2.),put(day(full_date),z2.));

Date pretty much must be text as you're showing here.

You could also create a custom format to do this directly, Look up Proc Format and date predicates as an exercise for the interested reader.

Occasional Contributor
Posts: 9

Re: Extracting month/day from dates

Thank you, that works perfectly. Much appreciated!

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 11530 views
  • 1 like
  • 2 in conversation