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?
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?
Hi @ccaudillo100,
would the MONTH function a possibility for you:
SAS Help Center: MONTH Function
Best
Markus
No need to make a new column. Just change the format. Use format MONNAME.
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
@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).
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
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?
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.);
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.