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


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.

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

2 REPLIES 2
ballardw
Super User

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.

kmaths
Calcite | Level 5

Thank you, that works perfectly. Much appreciated!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 2 replies
  • 41397 views
  • 2 likes
  • 2 in conversation