Desktop productivity for business analysts and programmers

Grouping by dates

Posts: 61

Grouping by dates

Hello all,

I just started using SAS at work and have been learning on my own for a few weeks before we get sent off to formal training. The question that I have is how can I group by a formatted date The field that I have is a date time field, and what I want to do is group that date by YYYYMMs format. Once my date is in the proper format, I'm unable to group by that format; SAS seems to revert back to the field's original format despite showing it in the format that I desire.

The only way I have gotten around this is to turn my date field into YYYMMs as a character field, but the obviously I lose some date functionality. Any suggestions?
Trusted Advisor
Posts: 2,127

Re: Grouping by dates

Posted in reply to mdavidson
Some SAS procedures use the formats, others ignore them. So the answer is: It depends.

For instance, PROC MEANS uses formats for the CLASS statement but not for the VAR statement. FREQ uses them throughout. REG ignores them for continuous variables. The DATA step generally ignores formats in expressions, but there are functions to allow you to get the formatted value [PUT(varname, format.), for instance].

You probably need to read the chapters on Dates and formats in the Base manual and scan the procedure manuals for 'format' in the PROC chapters that are of interest. For us to provide more specific suggestions, we'll need a more specific question.
Posts: 35

Re: Grouping by dates

Posted in reply to mdavidson
For grouping purposes, I just create a new variable to restrict my data.

Something like


will change 17Jun2008 to 1Jun2008. You will lose the day specific information, but when you format it as 200806, you will know that all the 200806 records will be grouped together.
Ask a Question
Discussion stats
  • 2 replies
  • 3 in conversation