BookmarkSubscribeRSS Feed
radha009
Quartz | Level 8

How to sort the dataset by month order

9 REPLIES 9
Astounding
PROC Star

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?

SuryaKiran
Meteorite | Level 14

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

Thanks,
Suryakiran
SHIWAGOLLA
Calcite | Level 5
SYNTAX PLEASE..
Kurt_Bremser
Super User

@SHIWAGOLLA wrote:
SYNTAX PLEASE..

Question, please.

Post your existing data and what you want to get out of it (dataset or report), also show what you already tried (code!).

HB
Barite | Level 11 HB
Barite | Level 11

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.));

 

Reeza
Super User

Vague questions = vague generic answers.

radha009
Quartz | Level 8

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;

Reeza
Super User

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. 

mariangela86
Fluorite | Level 6

Hi

you can use proc sql, order by, month() and case when for convert numeric month to char

for example::

 

*dataset input;
%let data_in=SASHELP.CITIDAY;
*campo date numerico DATE9.;

 

proc sql;
create table cityday0 as
select
a.date,
year(a.date) as year_num,
month(a.date) as month_num,
(case
when month(a.date) =01 then 'JAN'
when month(a.date) =02 then 'FEB'
when month(a.date) =03 then 'MAR'
when month(a.date) =04 then 'APR'
when month(a.date) =05 then 'MAY'
when month(a.date) =06 then 'JUN'
when month(a.date) =07 then 'JUL'
when month(a.date) =08 then 'AUG'
when month(a.date) =09 then 'SEP'
when month(a.date) =10 then 'OCT'
when month(a.date) =11 then 'NOV'
when month(a.date) =12 then 'DEC'
else 'None'
end) as mese_char,
day(a.date) as day_num
from &data_in. as a
order by year_num, month_num, mese_char, day_num;
quit;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 9 replies
  • 7721 views
  • 1 like
  • 8 in conversation