SAS Programming

DATA Step, Macro, Functions and more
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.

sas-innovate-white.png

Special offer for SAS Communities members

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.

 

View the full agenda.

Register now!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 18844 views
  • 1 like
  • 6 in conversation