DATA Step, Macro, Functions and more

Alternatives to get (Month Year Name together) from Numeric values?

Accepted Solution Solved
Reply
Contributor
Posts: 61
Accepted Solution

Alternatives to get (Month Year Name together) from Numeric values?

Hi, All,

I ran into a small problem which I need to get the month name spelled out together with the year, from a numeric date value. I can do it using catx function after obtaining the month name in two ways. However, I wonder if there are any better ways to do this, such as using only one function to get the result (e.g. January 2006). Please offer thoughts. Thanks.

Joe

/**/

data have;

length want1 want2 $25.;

input yearmonth;

yearmonth_d=input (put(yearmonth,6.), yymmn6.);

/*format yearmonth_d yymmd.;*/

format yearmonth_d monyy7.;

year =year(yearmonth_d);

month1=put(yearmonth_d,monname.); * 1st way to get the month;

want1=catx (' ', month1,year);

month2=put(yearmonth_d,worddate9.); * 2nd way to get the month;

want2=catx (' ', month2,year);

cards;

200601

200709

200812

;run;

/**/


Accepted Solutions
Solution
‎01-07-2015 11:28 PM
Super User
Posts: 10,018

Re: Alternatives to get (Month Year Name together) from Numeric values?

You can always customize the date or datetime format via proc format.

proc format;
picture fmt
 low-high='%B %Y'(datatype=date) ;
run;
data have;
input yearmonth;
yearmonth_d=input(put(yearmonth,6.), yymmn6.);
format yearmonth_d fmt20.;
cards;
200601
200709
200812
;run;

Xia Keshan

View solution in original post


All Replies
Contributor
Posts: 45

Re: Alternatives to get (Month Year Name together) from Numeric values?

Hi,

I have been digging through the documentation and cannot find what you are looking for which is really surprising because I thought one of the formats would surely give you just the month without an abreviation and the year. The best I could find is the WORDDATEw. format if you are okay with including the day: January 7, 2015.

I am curious if others are aware of any functions that return the date in the format you are requesting.

M.


Contributor
Posts: 61

Re: Alternatives to get (Month Year Name together) from Numeric values?

Posted in reply to MaikH_Schutze

Thanks, M. I am aware of this WORDDATEw. format. I really don't need the day information. But that is a good option. Thanks.

PROC Star
Posts: 7,467

Re: Alternatives to get (Month Year Name together) from Numeric values?

You could always parse what you want using the worddate format. e.g.,:

data have;

  length want3 $25.;

  input yearmonth yymmn6.;

  want3=substr(put(yearmonth,worddatx.),anyalpha(put(yearmonth,worddatx.)));

cards;

200601

200709

200812

;

Respected Advisor
Posts: 4,173

Re: Alternatives to get (Month Year Name together) from Numeric values?

You can always define your own picture format.

proc format;

  picture yearmonth (default=30)

    other='%C %Y' (datatype=date )

    ;

run;

data have;

  do yearmonth=200601,200709,200812;

    want=compbl(put(input(put(yearmonth,6.),yymmn6.),yearmonth.));

    output;

  end;

run;

Solution
‎01-07-2015 11:28 PM
Super User
Posts: 10,018

Re: Alternatives to get (Month Year Name together) from Numeric values?

You can always customize the date or datetime format via proc format.

proc format;
picture fmt
 low-high='%B %Y'(datatype=date) ;
run;
data have;
input yearmonth;
yearmonth_d=input(put(yearmonth,6.), yymmn6.);
format yearmonth_d fmt20.;
cards;
200601
200709
200812
;run;

Xia Keshan

Super Contributor
Posts: 435

Re: Alternatives to get (Month Year Name together) from Numeric values?

What does '%B %Y' indicates in your format?

Contributor
Posts: 45

Re: Alternatives to get (Month Year Name together) from Numeric values?

Hi,

Same question for Patrick and Xia. Can you please explain the processing of PROC format used in both of your examples?

Also, it still appears that both examples still use standard SAS formats to pull (or parse) out the pieces and then join them together in a new format. Isn't that the same as parsing the values and then concatenating them to get the desired result?

Thanks,

M.

Super User
Posts: 10,018

Re: Alternatives to get (Month Year Name together) from Numeric values?

Posted in reply to MaikH_Schutze

"Isn't that the same as parsing the values and then concatenating them to get the desired result?"

No. They are different . Patrick and I make a whole new format and transform it into character via put() or just assign it to the variable.

They are two different skill. Patrick's and mine would be better(i.e. no need to add SUBSTRING function )

Xia Keshan

Super User
Posts: 10,018

Re: Alternatives to get (Month Year Name together) from Numeric values?

You can find them at sas documentation.

%B

full month name, for example, January.

%Y

year with century as a four-digit decimal number (1970–2069), for example,

1994.

Xia Keshan

Occasional Contributor
Posts: 6

Re: Alternatives to get (Month Year Name together) from Numeric values?

Greetings!

The following SUGI doc not only explains the %B, it give a full table of Directives in Figure 1.

http://www2.sas.com/proceedings/sugi27/p101-27.pdf

This should help build a format for almost any date in almost any format.

Wayne Bell

PROC Star
Posts: 7,467

Re: Alternatives to get (Month Year Name together) from Numeric values?

Posted in reply to WayneBell

Wayne: a more up to date list can be found at: Base SAS(R) 9.2 Procedures Guide

Contributor
Posts: 61

Re: Alternatives to get (Month Year Name together) from Numeric values?

Thank you all.

I think i got some study to do next.Smiley Happy

🔒 This topic is solved and locked.

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

Discussion stats
  • 12 replies
  • 539 views
  • 6 likes
  • 7 in conversation