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. 

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 3 replies
  • 3972 views
  • 0 likes
  • 4 in conversation