Help using Base SAS procedures

Convert month number to name

Accepted Solution Solved
Reply
Contributor SKP
Contributor
Posts: 25
Accepted Solution

Convert month number to name

Hi,

 

I'm tyring to conver month number (e.g. 1,2,3) to name (e.g. JAN, FEB, MAR) in a field "Month" which is character field.

I tried below code;

 

Proc Format;

 INVALUE Month '1'='JAN', '2'='FEB','3'='MAR',

Run;

 

But it got errored out and error says that 'JAN' was not acceptable format.  I'm not sure what that means....

Does anyone how to fix this or know a batter to do?

 

Thanks,

SKP


Accepted Solutions
Solution
‎08-06-2016 02:44 AM
Super User
Posts: 19,791

Re: Convert month number to name

Put SET statement before you try to use the variables 😉

 

Your trying to to use it before it exists.

View solution in original post


All Replies
Super Contributor
Posts: 266

Re: Convert month number to name

You can try using format monname3.

 

 

data _null_;
format a monname3.;
a=today();
put a=;
run;

 

Contributor SKP
Contributor
Posts: 25

Re: Convert month number to name

Thank you for your reply.

I tried below and it returned as 'AUG' only.  I guess that's today's month....

Can you let me know where I can specify the dataset and field name?

 

Super User
Posts: 19,791

Re: Convert month number to name

The format is MONNAME. Apply it the same way you were planning to use your custom format. It will only work of you have a full SAS date, numeric with a date format not a character or number 1/2/3. 

Contributor SKP
Contributor
Posts: 25

Re: Convert month number to name

Thank you for your reply.  I understand that I should have SAS date field to use the MONNAME function, but still couldn't get what I need.

 

Let me clarify what I'm doing again.

 

In a data set "test", I have a date filed "purchase_date" which has date in MM/DD/YYYY format.  I wanted to add new field "purchase_month" in month name format (JAN, FEB etc)  based on "purchase_date". 

 

Right now, I did not know MONNAME function, thus I pulled month as numeric from "purchase_date" and tried to conver it to name.  But it sounds I should be able to pull month name (JAN, FEB) from "purchase_date".

Can you please let me know once again how I can do that with MONNAME function?

 

Thank you.

Super User
Posts: 19,791

Re: Convert month number to name

[ Edited ]

Since you want a new variable, create a new variable identical to the date and apply format. 

 

Data Step

 

Month = purchase_date;

format month MONNAME.;

 

SQL

purchase_date as month format=MONNAME.

 

Or convert to character month. 

 

Month=put(purchase_date, monname3.);

Contributor SKP
Contributor
Posts: 25

Re: Convert month number to name

I think I'm getting, but it's not quit working yet.

 

I tried your 1st code as

 

data newdataset;

 purchse_month=purchse_date

format purchse_month MONNAME3.;

set olddataset;

Run;

 

Then, I have . in the 1st cell and everything god shifted down by 1 row. 

Do you know why it got shifted?

 

Thanks. 

Solution
‎08-06-2016 02:44 AM
Super User
Posts: 19,791

Re: Convert month number to name

Put SET statement before you try to use the variables 😉

 

Your trying to to use it before it exists.

Contributor SKP
Contributor
Posts: 25

Re: Convert month number to name

It worked!!!!!!

Thank you very much for your help!!!!

 

And thank you for teaching me all other codes, as well.  I haven't tested all those yet, but will test them later to learn new codes.

 

SKP

 

 

Super User
Posts: 19,791

Re: Convert month number to name

[ Edited ]

There's no commas between the list of values. I also wouldn't recommend using the name month as its a function. 

 

If you convert it to a SAS date you can use MONNAME format. 

 

Proc format;
InValue $ monthFmt
'1' = 'Jan'
'2' = 'Feb'
...
;

 

Perhaps if you explain how you plan to use it we can suggest more efficient methods. 

Contributor SKP
Contributor
Posts: 25

Re: Convert month number to name

Thank you for your reply.

It did not work.

Where do you specify the dataset name?

 

I changed the field to "purchase_month", and had below.

Proc Format;

InValue $ purchase_monthFmt

'1' = 'JAN'

'2' = 'FEB'

'3' = 'MAR';

Run;

 

Did i have anything incorrect?

Super Contributor
Posts: 266

Re: Convert month number to name

You are using Invalue keyword that will create informat.

 

What I understand with your question that you want to convert 1 to Jan which can be done using format.  Be sure what do you want Format or Informat. 

 

Another point is you are considering 1 as character in your code. Ensure that your input varaible where you are applying format is charcter type

 

Proc Format;
Value $ purchase_monthFmt
'1' = 'JAN'
'2' = 'FEB'
'3' = 'MAR';
Run;
Contributor SKP
Contributor
Posts: 25

Re: Convert month number to name

It seems it ran but nothing returned.

In Log Summary, it says Format $PURCHASE_MONTH has been output.

But I would like to overwite it in the same dataset in purchase_month filed where I have 1,2,3 etc.

Is there other code I need to have?

 

Thank you.

☑ This topic is solved.

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

Discussion stats
  • 12 replies
  • 4423 views
  • 0 likes
  • 3 in conversation