DATA Step, Macro, Functions and more

Month dates from number to letters

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

Month dates from number to letters

Hi to everybody!

 

I'm working with date formats. I've got a nice data set with this but the forma of my dates are for example: 02/11/2016. I need to put first the day, then month and then year. Untill here everything is ok but I would like to put them in this format: 02/NOV/2016 and I can't!

I wrote "Month = put(date,monname3.);" and now I've converted my variable month from number to Words but I am not able to put that in the way I want "02/NOV/2016" "24/FEB/2013", etc.
 

DatePA= left(fecha);
Day = SUBSTR(fechapa,7,2);
Month = SUBSTR(fechapa,5,2);
Year = SUBSTR(fechapa,1,4);
Date = MDY (Month,Day,Year);
Month = put(date,monname3.);
format Date ddmmyy10.;

 

Thank you!


Accepted Solutions
Solution
‎11-09-2016 04:13 PM
Super User
Posts: 5,500

Re: Month dates from number to letters

Investigate whether the requirements can be a little bit flexible.  It's very easy to come close to what you are asking for:

 

m1 = put(date, date11.);

 

or

 

m2 = put(date, date9.);

View solution in original post


All Replies
Solution
‎11-09-2016 04:13 PM
Super User
Posts: 5,500

Re: Month dates from number to letters

Investigate whether the requirements can be a little bit flexible.  It's very easy to come close to what you are asking for:

 

m1 = put(date, date11.);

 

or

 

m2 = put(date, date9.);

Super User
Posts: 19,787

Re: Month dates from number to letters

Look at building a custom format, with PROC format. 

Its easier than you think Smiley Happy

 

Proc format;

picture ddmonyy other= "%0d/%b/%Y" (datatype=date);

run;

Super User
Posts: 11,343

Re: Month dates from number to letters

When dealing with dates it is often easier to actually use a SAS date valued variable an a format. Your request for / in the middle of the date with the month name is uncommon but doable with a custom format.

 

The following code creates a SAS date valued variable from a character variable that I think mimics your fechapa variable.

Then uses the custom format created with proc format to display the date as desired.

NOTE: the case of the letters in the string '%d/%b/%Y' is critical as changing the Y to y will result in a 2 digit year and a %B instead of %b is the full name not the 3-letter abbreviation. The % tells proc format the values are directives and the datatype tells SAS what ranges of values to expect.

data example;
   x='20160425';
   y=input(x,yymmdd8.);
   format y date9.;
run;

proc format library=work;
picture DateSlash  (default=12)
  low-high= '%d/%b/%Y'  ( datatype=date)
;
run;

proc print data=example;
   var y;
   format y dateslash.;
run;

So once you have your Date variable you use the format dateslash as needed. Note, since this is a custom format permanent association of the format means that you should place the format in a location that is always in the FMTSEARCH path OR rerun the proc format as needed.

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 272 views
  • 0 likes
  • 4 in conversation