Desktop productivity for business analysts and programmers

How do I change SAS Datetime value to Month-Year value?

Reply
Learner
Posts: 1

How do I change SAS Datetime value to Month-Year value?

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.

Super User
Posts: 5,391

Re: How do I change SAS Datetime value to Month-Year value?

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

Data never sleeps
Super User
Super User
Posts: 7,732

Re: How do I change SAS Datetime value to Month-Year value?

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;
Super User
Posts: 19,194

Re: How do I change SAS Datetime value to Month-Year value?

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. 

Ask a Question
Discussion stats
  • 3 replies
  • 675 views
  • 0 likes
  • 4 in conversation