The Format monname returns the name of month and expects a valid sas-date as input.
I don't think that a format translating month names to numbers exists.
You could do something like:
Easier way: write a format for doing the conversation or use Google ...
Use the Monname Format
A date value in SAS is stored as the number of days since 1/1/1960. You then apply a date format for printing so the internal value becomes human readable as a date.
You can also convert a text string representing a date to a SAS date value. You do this by using the input function together with an informat which instructs SAS how to interpret the source string.
So now if your source string is only a month name: How should SAS "know" which calendar day and year to use for conversion into a SAS date value. If you know this then it's possible to write code. Something like the following would work:
Data ex;
Date='april';
New=input(cats('01',substr(date,1,3),'2019'),date9.);
format new date9.;
Run;
If your months followed SAS conventions, you could use a SAS-supplied informat:
data test;
month_text = "apr";
month_num = month(input(strip(month_text) !! '2018',monyy10.));
run;
If at least the first three letters followed SAS conventions, you can use substr():
data test;
month_text = "april";
month_num = month(input(substr(month_text,1,3) !! '2018',monyy10.));
run;
If that is not the case, use a select() block in your data step:
select (month_text);
.......
when ('april') month_num = 4;
........
end;
@thanikondharish wrote:
Data ex;
Date='april';
New=input(date,monname.);
Run;
If I run above I am getting error there no informat I know
But how to convert
Sometimes you either have to write if then else/ select code OR create a custom informat.
proc format library = work; invalue mon (upcase) "JANUARY" =1 "FEBRUARY" =2 "MARCH" =3 "APRIL" =4 "MAY" =5 "JUNE" =6 "JULY" =7 "AUGUST" =8 "SEPTEMBER" =9 "OCTOBER" =10 "NOVEMBER" =11 "DECEMBER" =12 other=. ; run; Data ex; Date='april'; New= input(date,mon.); Run;
Note that the UPCASE option on the informat converts any text to upper case before comparing with the values in the range of the informat. That way inconsistent values like 'april' 'April' 'aPRIL' will all get handled.
And after 200+ posts it is time to learn to use the forum tools for posting code and logs such as the code box opened with the forum's {I} or "running man" icon.
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.