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

Hi All,

 

I am trying to getting month and year from a datetime. field. While I manage to get May2017 from '02MAY17:07:57:59', I need it in the format 052017 i.e month expressed in digits. Can someone please guide how I can achieve it? Please find below the code I tried.

 

DATA date_constants;
datetime = '02MAY17:07:57:59'dt; /* This is a datetime constant */
run;


TITLE “Unformatted Constants”;
PROC PRINT DATA=date_constants;
VAR datetime;
run;


TITLE “Formatted Constants”;
PROC PRINT DATA=date_constants;
VAR datetime;
FORMAT datetime datetime.;
run;

 

data date_tril;
set date_constants;
serve = datepart(datetime);
format serve monyy7.;


PROC PRINT DATA=date_tril;
run;

 

Thanks,

Akhilesh Joshi

1 ACCEPTED SOLUTION

Accepted Solutions
collinelliot
Barite | Level 11

To be clear, you're not trying to get the month and year from the field. You're extracting the date and then formatting it. I think what you want is mmyyn6. for your format.

View solution in original post

4 REPLIES 4
collinelliot
Barite | Level 11

To be clear, you're not trying to get the month and year from the field. You're extracting the date and then formatting it. I think what you want is mmyyn6. for your format.

akhileshJoshi
Calcite | Level 5

Hi Colin,

 

That format worked!

 

Thanks much!

Cynthia_sas
SAS Super FREQ

Hi:

  Once you use the DATEPART function, then it should only be a matter of picking the right format, as shown below. As you can see the same date is formatted multiple ways using different formats.

 

cynthia

use_format_nosep.png

akhileshJoshi
Calcite | Level 5

Awesome ,Cynthia! Thanks a lot!

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 4 replies
  • 14876 views
  • 2 likes
  • 3 in conversation