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";
@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
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.
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.
@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
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.