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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Put SET statement before you try to use the variables 😉

 

Your trying to to use it before it exists.

View solution in original post

12 REPLIES 12
RahulG
Barite | Level 11

You can try using format monname3.

 

 

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

 

SKP
Calcite | Level 5 SKP
Calcite | Level 5

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?

 

Reeza
Super User

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. 

SKP
Calcite | Level 5 SKP
Calcite | Level 5

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.

Reeza
Super User

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.);

SKP
Calcite | Level 5 SKP
Calcite | Level 5

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. 

Reeza
Super User

Put SET statement before you try to use the variables 😉

 

Your trying to to use it before it exists.

SKP
Calcite | Level 5 SKP
Calcite | Level 5

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

 

 

Reeza
Super User

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. 

SKP
Calcite | Level 5 SKP
Calcite | Level 5

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?

RahulG
Barite | Level 11

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;
SKP
Calcite | Level 5 SKP
Calcite | Level 5

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 12 replies
  • 47603 views
  • 0 likes
  • 3 in conversation