BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
ccaudillo100
Obsidian | Level 7

Hello,

In my table I have the date in date9. I am wanting to make a new column with just the month. Any examples on how to quickly do this?

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Month number? Month name?

Just the value or format the variable to look like just the month?

data demo;
mydate=today();
month_num = month(mydate);
month_name = put(mydate, monname3.);
month_num_fmt=mydate;
month_nam_fmt=mydate;
format mydate date9. month_num_fmt month. month_nam_fmt monname3.;
run;

proc print data=demo;
run;

@ccaudillo100 wrote:

Hello,

In my table I have the date in date9. I am wanting to make a new column with just the month. Any examples on how to quickly do this?


 

View solution in original post

7 REPLIES 7
MarkusWeick
Barite | Level 11

Hi @ccaudillo100,

would the MONTH function a possibility for you:

SAS Help Center: MONTH Function

Best

Markus

Please keep the community friendly.
Like posts you agree with or like. Mark helpful answers as “accepted solutions”. Generally have a look at https://communities.sas.com/t5/Getting-Started/tkb-p/community_articles
PaigeMiller
Diamond | Level 26

No need to make a new column. Just change the format. Use format MONNAME.

--
Paige Miller
ccaudillo100
Obsidian | Level 7

I am needing to make it a new column for a visual I am making. I need to keep the date9 because I delete information if it is before or after a certain date

PaigeMiller
Diamond | Level 26

@ccaudillo100 wrote:

I am needing to make it a new column for a visual I am making. I need to keep the date9 because I delete information if it is before or after a certain date


You can use the variable with the MONNAME format in your visual. You can still delete the information if it is before or after a certain date, even after you change the format. Changing the format does not change the underlying (unformatted) value, and SAS always uses the underlying (unformatted) value to do logical operatoins. So if you use the MONNAME format, you can still do things like

 

where date < '01JAN2019'd

 

and it will work just fine (because SAS uses the unformatted value always 100% of the time).

--
Paige Miller
Reeza
Super User

Technically not required, you can use the date in different formats in different places. 

 

proc sgplot data=sashelp.stocks;
where stock='IBM' and date >= '06Jun1994'd;
series x=date y=open;
series x=date y=close;
format date monname3.;
run;

@ccaudillo100 wrote:

I am needing to make it a new column for a visual I am making. I need to keep the date9 because I delete information if it is before or after a certain date


 

Reeza
Super User

Month number? Month name?

Just the value or format the variable to look like just the month?

data demo;
mydate=today();
month_num = month(mydate);
month_name = put(mydate, monname3.);
month_num_fmt=mydate;
month_nam_fmt=mydate;
format mydate date9. month_num_fmt month. month_nam_fmt monname3.;
run;

proc print data=demo;
run;

@ccaudillo100 wrote:

Hello,

In my table I have the date in date9. I am wanting to make a new column with just the month. Any examples on how to quickly do this?


 

Tom
Super User Tom
Super User

Do you need a new variable or are you just looking to print or group by the month in some report (or analysis)?

If the later then just use a different format with the existing variable.

 

proc means data=have;
  class date ;
  format date yymm7.;
run;

If the former than you could just map the date to the first of the month.  That way the value is still a date, but all of the observations from the same month will have the same value.

 

 

month = intnx('month',date,0,'b');
format month yymm7.;

Should it include the year and the month? Or just the month?  Do you want the name of the month? Or the number?

Do you want a string?  You could use the PUT() function with the appropriate format.

month_string=put(date,yymm7.);

 

 

 

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 7 replies
  • 672 views
  • 2 likes
  • 5 in conversation