DATA Step, Macro, Functions and more

sort by quarter

Reply
Occasional Contributor
Posts: 18

sort by quarter

I have data set with SAS date variable spanning several years.
I want to create a variable that contains the year and the quarter (e.g 2010Q2), which can then be used to sort by.

Can anyone help?

Masoud
SAS Super FREQ
Posts: 8,864

Re: sort by quarter

Hi:
Read about the various YYQ formats. For example, a SAS date value for 03/15/2006 could be used to create a character variable called QTRVAL with this code:
[pre]
qtrval = put(date,yyq6.);
[/pre]

And the resulting value would be: 2006Q1

The documentation on the various formats is here:
http://support.sas.com/documentation/cdl/en/lrdict/63026/HTML/default/viewer.htm#/documentation/cdl/...
http://support.sas.com/documentation/cdl/en/lrdict/63026/HTML/default/viewer.htm#/documentation/cdl/...
http://support.sas.com/documentation/cdl/en/lrdict/63026/HTML/default/viewer.htm#/documentation/cdl/...

cynthia
Occasional Contributor
Posts: 18

Re: sort by quarter

Posted in reply to Cynthia_sas
Thanks Cynthia.
Valued Guide
Posts: 2,177

Re: sort by quarter

Masoud

what difference do you expect when sortng by (internal) sas date and sorting by quarter?

just interested
Peterc
Super Contributor
Super Contributor
Posts: 3,174

Re: sort by quarter

With a multiple-variable BY statement, the order of observations that have date values throughout a given quarter, may get sorted differently if only considering the "formatted" value sort-sequence, presuming the BY statement has additional variables to the right of the date variable itself.

Scott Barry
SBBWorks, Inc.
Occasional Contributor
Posts: 18

Re: sort by quarter

I am using the quarter variable to for proc summary's class variable.

Masoud
Respected Advisor
Posts: 3,799

Re: sort by quarter

You can use the CLASS statement option GROUPINTERNAL to have you cake and eat it too.
Occasional Contributor
Posts: 18

Re: sort by quarter

Posted in reply to data_null__
Thank you all.
I will have to investigate and learn.

Masoud
Ask a Question
Discussion stats
  • 7 replies
  • 272 views
  • 0 likes
  • 5 in conversation