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

Hi there,

 

I am currently encountering a problem, I have an attribute date, the variable is a date type and the format is like 01MAY2018.

I want to use substr to extract only the month ie.May for this attribute for future analysis; however, sas told me that I need to convert data type first, but I couldn't find the way to convert this specific format to a character variable. Any thoughts?

 

Thanks a lot

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Once a variable is defined as numeric, it stays numeric forever.  So when you say it's a "date type" that probably means it's a numeric value on SAS's date scale and cannot be transformed into a character.

 

You can fairly easily create a character variable, based on the date's value.  For example:

 

char_mon = put(datevar, monname.);

 

Or perhaps:

 

char_mon = substr(put(datevar, date9.), 3, 3);

 

Here's a reference that might be helpful, depending on what it means to extract the month only:

 

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000201049.htm

 

View solution in original post

6 REPLIES 6
ForrestYao
Fluorite | Level 6

Hi there,

 

I am currently encountering a problem, I have an attribute date, the variable is a date type and the format is like 01MAY2018.

I want to use substr to extract only the month ie.May for this attribute for future analysis; however, sas told me that I need to convert data type first, but I couldn't find the way to convert this specific format to a character variable. Any thoughts?

 

Thanks a lot

Ashok3395
Fluorite | Level 6

Hi , 

 

You try your query using the below example.

 

data x;
dt = symget ('runasofdate');
rndt = cats("'",dt,"'");
call symput ('rndt',rndt);
mnth = substr(dt,1,6);
call symput('m_key',strip(mnth));
run;

Regards,

Ashok Arunachalam

jebjur
SAS Employee

You can use a combination of the PUT function (converts numeric to character) with the SUBSTR function to pull out only the month from the formatted date value:

 

data test;
input dt date9.;
format dt date9.;
month=SUBSTR(PUT(dt,date9.),3,3);
cards;
01MAY2018
01JUN2018
01JUL2018
01AUG2018
01SEP2018
01OCT2018
;
run;

ForrestYao
Fluorite | Level 6

Hi there,

 

Thanks for your reply, it works!

Astounding
PROC Star

Once a variable is defined as numeric, it stays numeric forever.  So when you say it's a "date type" that probably means it's a numeric value on SAS's date scale and cannot be transformed into a character.

 

You can fairly easily create a character variable, based on the date's value.  For example:

 

char_mon = put(datevar, monname.);

 

Or perhaps:

 

char_mon = substr(put(datevar, date9.), 3, 3);

 

Here's a reference that might be helpful, depending on what it means to extract the month only:

 

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000201049.htm

 

ForrestYao
Fluorite | Level 6

Thanks a lot, yeah, using your method successfully extracts only the month part and convert it to the character variable.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 6 replies
  • 8868 views
  • 1 like
  • 4 in conversation