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;
/**/
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
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.
Thanks, M. I am aware of this WORDDATEw. format. I really don't need the day information. But that is a good option. Thanks.
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
;
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;
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
What does '%B %Y' indicates in your format?
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.
"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
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
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
Wayne: a more up to date list can be found at: Base SAS(R) 9.2 Procedures Guide
Thank you all.
I think i got some study to do next.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.