DATA Step, Macro, Functions and more

month change in numaric

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

month change in numaric

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";


Accepted Solutions
Solution
‎02-08-2017 09:34 AM
Super Contributor
Posts: 474

Re: month change in numaric

@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


All Replies
Super User
Super User
Posts: 7,942

Re: month change in numaric

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.

New Contributor
Posts: 4

Re: month change in numaric

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. -##
Super User
Super User
Posts: 7,942

Re: month change in numaric

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.

Solution
‎02-08-2017 09:34 AM
Super Contributor
Posts: 474

Re: month change in numaric

@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

Super User
Posts: 5,503

Re: month change in numaric

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;

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 160 views
  • 2 likes
  • 4 in conversation