BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jiangmi
Calcite | Level 5

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;

/**/

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

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

12 REPLIES 12
MaikH_Schutze
Quartz | Level 8

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.


jiangmi
Calcite | Level 5

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

art297
Opal | Level 21

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

;

Patrick
Opal | Level 21

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;

Ksharp
Super User

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

Babloo
Rhodochrosite | Level 12

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

MaikH_Schutze
Quartz | Level 8

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.

Ksharp
Super User

"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

Ksharp
Super User

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

WayneBell
Calcite | Level 5

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

art297
Opal | Level 21

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

jiangmi
Calcite | Level 5

Thank you all.

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 12 replies
  • 2420 views
  • 6 likes
  • 7 in conversation