DATA Step, Macro, Functions and more

Convert numeric month number into "mmm" string

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 15
Accepted Solution

Convert numeric month number into "mmm" string

Hello,

 

I have dates being generated in a numeric format of yymm (eg. 1706 = June2017).

 

I have been trying to substring it to grab the month and then convert it into "mmm" using monname3. but with no success. In the example above I am trying to grab "Jun".

 

As a test I have tried:

 

data test ;
mon=06 ;
month=put(mon,monname3.) ;
run ;

 

However this always returns "Jan" in month?! Very confused, any help is much appreciated.

Declan


Accepted Solutions
Solution
‎12-11-2017 12:41 PM
Super User
Posts: 13,950

Re: Convert numeric month number into "mmm" string

Posted in reply to DeclanBall

Ugly but is this what you want:

data junk;
   date = 1706;
   mon = put(mdy(input(substr( put(date,4.) ,3),2.),1,2000),monname3.);
run;

View solution in original post


All Replies
Super User
Super User
Posts: 9,866

Re: Convert numeric month number into "mmm" string

Posted in reply to DeclanBall

Dates, regardless of how you format them, are stored as number of days since a certain timepoint.  Thus they always need to contain the three parts Year, month, and day, and from that the number of days can be derived.  With this number of days you can then apply formats to display that data differently.  Now when you say display 6 as a date format, it assumes the 6 means 6 days and converts that to be 6 days from the default timepoint, so always going to be Jan in this instance as 01JAN1966 (or whatever it is, can't think off top of my head) is 07Jan1966, displayed as month is Jan.  What you want is a date:

data test ;
  mon='01jan2017'd ;
  month=put(mon,monname3.) ;
run ;
Occasional Contributor
Posts: 15

Re: Convert numeric month number into "mmm" string

Ok thanks, that I didn't know.

My issue is that my dates are being given to me in the numeric YYMM format, I have no control over that. How do I get from that to a position where I can extract MM as "Jun" etc?
Super User
Super User
Posts: 9,866

Re: Convert numeric month number into "mmm" string

Posted in reply to DeclanBall

So:

data want;
  have=1706;
  want=input(cats(put(have,4.),"01"),yymmdd6.);
  format want monname3.;
run;
Solution
‎12-11-2017 12:41 PM
Super User
Posts: 13,950

Re: Convert numeric month number into "mmm" string

Posted in reply to DeclanBall

Ugly but is this what you want:

data junk;
   date = 1706;
   mon = put(mdy(input(substr( put(date,4.) ,3),2.),1,2000),monname3.);
run;
Occasional Contributor
Posts: 15

Re: Convert numeric month number into "mmm" string

Due to not having CAT function (base SAS), this has saved my day. Many thanks everyone for your help
Super User
Posts: 24,027

Re: Convert numeric month number into "mmm" string

Posted in reply to DeclanBall

CATT is in Base SAS....for a long time.

Occasional Contributor
Posts: 15

Re: Convert numeric month number into "mmm" string

I must not have even base SAS then, log reports that function cannot be accessed Smiley Sad
Super User
Posts: 24,027

Re: Convert numeric month number into "mmm" string

Posted in reply to DeclanBall

DeclanBall wrote:
I must not have even base SAS then, log reports that function cannot be accessed Smiley Sad

You couldn't use the previous answer either if you don't have BASE SAS. What are you using?

Occasional Contributor
Posts: 15

Re: Convert numeric month number into "mmm" string

[ Edited ]

Unsure of what version of SAS I am using, however this is my log:

 

error.PNG

Super User
Posts: 24,027

Re: Convert numeric month number into "mmm" string

Posted in reply to DeclanBall

What do you get for this:

 

%put &sysvlong;

Occasional Contributor
Posts: 15

Re: Convert numeric month number into "mmm" string

8.02.02MOP020601
Super User
Posts: 24,027

Re: Convert numeric month number into "mmm" string

Posted in reply to DeclanBall

SAS 8 was released in 1999 so that's 18 years old.....so you have an Ancient* version of SAS. 

18 year olds are pretty unruly these days.

 

PS. That's the technical term Smiley Wink 

 

https://blogs.sas.com/content/iml/2013/08/02/how-old-is-your-version-of-sas-release-dates-for-sas-so...

Super User
Posts: 24,027

Re: Convert numeric month number into "mmm" string

Posted in reply to DeclanBall

Convert it to a SAS date using INPUT and PUT.

 

data have;

x= 1706;

y = put(x, 8. -l);

z = input(catt('20', y), yymmn6.);

monName = put(z, monname3.);
format z date9.;

run;

proc print data=have;run;

 

☑ This topic is solved.

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

Discussion stats
  • 13 replies
  • 966 views
  • 0 likes
  • 4 in conversation