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
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
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
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
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;
Hi there,
Thanks for your reply, it works!
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
Thanks a lot, yeah, using your method successfully extracts only the month part and convert it to the character variable.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.
Ready to level-up your skills? Choose your own adventure.