How only month variable can be sorted in chronological order?

Reply
New Contributor
Posts: 4

How only month variable can be sorted in chronological order?

Hi,

Please tell me what to use with proc sort to get months in chronological order.

data a;

length month $ 12;

input month $;

datalines;

Febuary

January

March

May

June

April

July

September

August

October

December

November

;

run;

Super User
Super User
Posts: 7,039

Re: How only month variable can be sorted in chronological order?

Posted in reply to nitin_k24

Convert it to a month number instead of the name. Then you can sort by that value.

monthnum = month(input('01'||substr(month,1,3)||'1960',date9.));

New Contributor
Posts: 4

Re: How only month variable can be sorted in chronological order?

Thanks for the reply Tom..

Is there any statement from which we can directly sort the months in chronological order.

Respected Advisor
Posts: 4,919

Re: How only month variable can be sorted in chronological order?

Posted in reply to nitin_k24

The short answer is No. But there are many indirect ways. With SQL for example :


data mn;
length monthName $12;
input monthNumber monthName;
datalines;
1 January
2 February
3 March
4 April
5 May
6 June
7 July
8 August
9 September
10 October
11 November
12 December
;

data a;
length month $12;
input month;
datalines;
February
January
March
May
June
April
July
September
August
October
December
November
;

proc sql;
select month from a left join mn on a.month=mn.monthName order by monthNumber;
quit;

PG

PG
New Contributor
Posts: 3

Re: How only month variable can be sorted in chronological order?

Posted in reply to nitin_k24

You may use the SQL statement as illustrated below.

Thanks, ZT

data MonthList;
format MonthName $12.;
input MonthName;
datalines;
December
November
October
September
August
July
June
May
April
March
February
January
;
run;

PROC SQL NOPRINT;
  CREATE TABLE MonthSorted(drop=MonthNum) AS SELECT month(input('01'||substr(MonthName,1,3)||'00',date.))
AS MonthNum, MonthName From MonthList ORDER BY MonthNum;
QUIT;

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