DATA Step, Macro, Functions and more

How to sort the dataset by month order

Reply
Contributor
Posts: 70

How to sort the dataset by month order

How to sort the dataset by month order

Super User
Posts: 6,903

Re: How to sort the dataset by month order

Does your data set contain a variable named MONTH?  If so, what's in it?

 

You might want to add a similar variable for the YEAR.  Probably, you would want December 2017 to sort earlier than January 2018, so  you would need both the month and year.

 

So what do you have already?

PROC Star
Posts: 618

Re: How to sort the dataset by month order

Use PROC SQL with ORDER BY clause with MONTH() function.

Thanks,
Suryakiran
Super Contributor
Super Contributor
Posts: 266

Re: How to sort the dataset by month order

The answer appears to be convert the month name to a month number and sort that. 

 

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

 

Super User
Posts: 23,958

Re: How to sort the dataset by month order

Vague questions = vague generic answers.

Contributor
Posts: 70

Re: How to sort the dataset by month order

my below data table has month and counts. month data displays dynamically it changes every month. To create SG Plot group graph i am transpose the data. To do transpose by month it gives error as sort the month. once i sorted the month is displaying in alphabetic order and my graph x asis names are displaying in same order. I am expecting the month order should be jan,feb and march

 

MonthTEST1TEST2TEST3TEST4TEST5TEST6TEST7TEST8
January284238421231287217917281
February296306556181358250773189
March2772455682783292271091229

 

data dash.three_Month_Trend2;
set dash.three_Month_Trend;
run;

proc sort data=dash.three_Month_Trend2;
by Month;
run;

proc transpose data=dash.three_Month_Trend2 out=dash.three_Month_Trend2;
by month;
   run;

data dash.three_Month_Trend2;
  set dash.three_Month_Trend2 (rename=(col1=count));
  BTO=_LABEL_;
  drop _NAME_ _LABEL_;  
run;

Super User
Posts: 23,958

Re: How to sort the dataset by month order

January, February would be sorted alphabetically. 

 

Either convert to numeric, use IF/THEN statements and apply a format or consider using a SAS Date instead of the value 'January'. 

If it's a SAS date with the MonNAME. format it will still show as January. 

Ask a Question
Discussion stats
  • 6 replies
  • 190 views
  • 0 likes
  • 5 in conversation