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
Put SET statement before you try to use the variables 😉
Your trying to to use it before it exists.
You can try using format monname3.
data _null_;
format a monname3.;
a=today();
put a=;
run;
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?
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.
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.
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.);
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.
Put SET statement before you try to use the variables 😉
Your trying to to use it before it exists.
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
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.
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?
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;
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.
Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.
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.
Ready to level-up your skills? Choose your own adventure.