BookmarkSubscribeRSS Feed
thanikondharish
Calcite | Level 5
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
5 REPLIES 5
andreas_lds
Jade | Level 19

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-format don'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 ...

Patrick
Opal | Level 21

@thanikondharish 

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;
Kurt_Bremser
Super User

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;
ballardw
Super User

@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.

Ready to join fellow brilliant minds for the SAS Hackathon?

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!
How to Concatenate Values

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.

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
  • 5 replies
  • 16614 views
  • 1 like
  • 6 in conversation