- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Date='april';
New=input(date,monname.);
Run;
If I run above I am getting error there no informat I know
But how to convert
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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:
- concatenate '01', the first three chars of the month-name and a year
- use that string in input-function with date9 as format to get a date
then use put-function with the date and monname-formatdon't know why is thought that the format monname is of any use here ... @Kurt_Bremser pointed out that using month-function is the tool to be used
Easier way: write a format for doing the conversation or use Google ...
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Use the Monname Format
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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.