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?
Use PROC SQL with ORDER BY clause with MONTH() function.
@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!).
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.));
Vague questions = vague generic answers.
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
Month | TEST1 | TEST2 | TEST3 | TEST4 | TEST5 | TEST6 | TEST7 | TEST8 |
January | 284 | 238 | 421 | 231 | 287 | 217 | 917 | 281 |
February | 296 | 306 | 556 | 181 | 358 | 250 | 773 | 189 |
March | 277 | 245 | 568 | 278 | 329 | 227 | 1091 | 229 |
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;
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.
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.