Help using Base SAS procedures

proc SQL order by a negative date column?

Reply
Occasional Contributor
Posts: 5

proc SQL order by a negative date column?

Hi,

Can a order by followed by a negative date column?

 

I have a code copied from somewhere like

 

PROC SQL;
  CREATE TABLE mcapData AS
  SELECT a.permno,
  a.date,
  a.mcap
  FROM retData AS a
 

ORDER

BY a.permno,

      -a.date;
  QUIT;

 

Is that a negative sign or just some error?

Respected Advisor
Posts: 4,654

Re: proc SQL order by a negative date column?

It is just a trick which should be written

 

BY a,permno, a.date DESC;

 

 

PG
Occasional Contributor
Posts: 5

Re: proc SQL order by a negative date column?

Thanks! You mean I can write like that but better use DESC?

Respected Advisor
Posts: 4,654

Re: proc SQL order by a negative date column?

DESC states what you want more clearly. The next person looking at the query will not wonder what you really meant to do.

PG
Occasional Contributor
Posts: 5

Re: proc SQL order by a negative date column?

Could you tell me whether SAS allows to use like that? 

Respected Advisor
Posts: 4,654

Re: proc SQL order by a negative date column?

Yes, you can ORDER BY a SQL expression. Check the Documentation.

PG
Respected Advisor
Posts: 4,654

Re: proc SQL order by a negative date column?

Added explanation. SAS dates are just numbers. Under the hood, the date 01JAN2010 is just 18263. So -date is also just a number; -18263 when viewed as a date is 31DEC1909. When SAS is sorting by a SAS date, it is in fact just sorting numbers.

 

Hope this helps.

PG
Occasional Contributor
Posts: 5

Re: proc SQL order by a negative date column?

Hi,Thank you very much ! I understand it now.

Ask a Question
Discussion stats
  • 7 replies
  • 373 views
  • 0 likes
  • 2 in conversation