BookmarkSubscribeRSS Feed
aj211191
Calcite | Level 5

Hello,

I am trying to convert datetime formats into month-year format in SAS and use them to sum a column by month and year. I have tried using 100*YEAR(DATEPART(DATE_VALUE))+MONTH(DATEPART(DATE_VALUE)) and it does work. But when I try to plot them on a graph, the dates are considered as numerical values which results in some random numbers on the horizontal axis. 

 

I have used the MMMYY formats, but those only format the dates into MONTH-YEAR format, they dont actually change the values of the date, and it wont let me group the columns by month and year, since their actual values are still unique.

3 REPLIES 3
LinusH
Tourmaline | Level 20

Then use the format in a put() function, the result is stored as a char: WYSIWYG.

Data never sleeps
RW9
Diamond | Level 26 RW9
Diamond | Level 26

It is always a good idea to post test data (in a datastep) of what you have, and what you want the output to look like.  I have taken a guess at the below:

data have;
  thedt="01JAN2014:01:00:00"dt; val=6; output;
  thedt="01JAN2014:06:10:00"dt; val=18; output;
  thedt="04MAR2014:03:00:00"dt; val=6; output;
  thedt="15MAR2014:04:00:00"dt; val=7; output;
  thedt="12JAN2015:01:00:00"dt; val=5; output;
  thedt="14JAN2014:01:00:00"dt; val=10; output;
run;

proc sql;
  create table WANT as
  select  YEAR,
          MONTH,          
          sum(VAL) as RESULT
  from    (select year(datepart(THEDT)) as YEAR,month(datepart(THEDT)) as MONTH,VAL from HAVE) 
  group by YEAR,MONTH;
quit;
Reeza
Super User

What do you mean it considers the actual date? 

 

Proc SQL doesn't recognize formats when using Group By, but proc means and proc freq will. 

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 4624 views
  • 0 likes
  • 4 in conversation