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

Hi I'm tried  to convert month (e.g. JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC) this one change's to numaric month 01,02,03....etc please tell me how to change in easy method .   i tried this one below.

 

if Month="JAN" then m="01";
if Month="FEB" then m="02";
if Month="MAR" then m="03";
if Month="APR" then m="04";
if Month="MAY" then m="05";
if Month="JUN" then m="06";
if Month="JUL" then m="07";
if Month="AUG" then m="08";
if Month="SEP" then m="09";
if Month="OCT" then m="10";
if Month="NOV" then m="11";
if Month="DEC" then m="12";

1 ACCEPTED SOLUTION

Accepted Solutions
DanielSantos
Barite | Level 11

@RW9 code is the right answer to your question.

 

If you want m to be a char variable instead, apply the put function to the result like below:

 

data want;
  set have;
  m=put(month(input(cats('01',month,'2010'),date9.)),z2.);
run;

 

Danile Santos @ www.cgd.pt

View solution in original post

5 REPLIES 5
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Follow the guidance you will see at the bottom of where you post.  Post example test data in the form of a datastep.  This is critical for us to see as we don't know what you are working with.  For example, if you data is a numeric SAS date, you could simply use the month() function to extract month name.  This being know, if you have a character variable with month, then thinking logically you could append 01 and 2010 to that month, create a date variable and then extract using the month() function.  As I have no test data I am going to assume month variable is character:

data want;
  set have;
  m=month(input(cats('01',month,'2010'),date9.));
run;

You could also create a format for it - though I personal avoid compiled anything as much as possible so would go with the above.

teja5959
Fluorite | Level 6
Thanks for quick reply!

Am working with clinical data, char_variable have months name as "JAN"
etc.. i want to convert it to numeric version of months to other variable,
please let me know how it can be done in data step

Thanks again!
Teja

##- Please type your reply above this line. Simple formatting, no
attachments. -##
RW9
Diamond | Level 26 RW9
Diamond | Level 26

You will want to try the datastep I posted which does exactly what you ask for.

 

As a note, you are getting data from a database in date parts yes.  If so then I recommend you get to one of the CDISC models, i.e. have a character date field which is the various parts of the date in ISO format, and also if you doing an analysis model then have a numeric date variable as well - this gives you the best of both worlds.

DanielSantos
Barite | Level 11

@RW9 code is the right answer to your question.

 

If you want m to be a char variable instead, apply the put function to the result like below:

 

data want;
  set have;
  m=put(month(input(cats('01',month,'2010'),date9.)),z2.);
run;

 

Danile Santos @ www.cgd.pt

Astounding
PROC Star

If I understand correctly, M is coming out as numeric when you would like it to be character.  If that is the problem, your code is correct as is.  The problem is that your data set already contains M and it is already defined as numeric.  You can change this by getting rid of the old M:

 

data want;

set have (drop=M);

* All your IF/THEN statements here;

run;

 

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