Hi,
I have a table that has a month and a year in a character format.. when I use proc gbarline function to draw the chart it display bars in the following sequence
2012-1
2012-10
2012-11
2012-12
2012-2
2012-3
and so on (obviously assuming that it's a character and not a date)
Here is the code that I've used
ROC GBARLINE DATA=WORK.INJ_SUMMARY_2
;
BAR YEAR_MONTH
/
SUMVAR=VOLUME INSIDE=SUM
SUBGROUP=PRODUCT
FRAME TYPE=SUM
COUTLINE=BLACK
RAXIS=AXIS1
MAXIS=AXIS2
LEGEND=Legend1
;
PLOT / SUMVAR=ORDERS
TYPE=SUM
AXIS=AXIS3
LEGEND=LEGEND2
;
may I know how to sort bars so that it should show like this
2012-1
2012-2
2012-3
2012-4
and so on
Thanks
If you provide a date variable rather than year month in character that's probably the easiest and will sort automatically, might need to go back a few steps.
I typically use my date variable and the yearmon7. format because it's easy to read.
If you provide a date variable rather than year month in character that's probably the easiest and will sort automatically, might need to go back a few steps.
I typically use my date variable and the yearmon7. format because it's easy to read.
or you may change your date to:
data have;
input dt $;
cards;
2012-1
2012-10
2012-11
2012-12
2012-2
2012-3
;
data have;
set have;
length newdt $7;
newdt=catx('-',scan(dt,1,'-'),put(scan(dt,2,'-')*1,z2.));
proc sort data=have;by newdt;run;
proc print;run;
Obs dt newdt
1 2012-1 2012-01
2 2012-2 2012-02
3 2012-3 2012-03
4 2012-10 2012-10
5 2012-11 2012-11
6 2012-12 2012-12
You can also try using SGPLOT procedure, with an overlay of a VBAR and VLINE. Make sure the data is in the correct order in the data set, and set the XAXIS discrete order to data.
1.)
Create new variable in dataset that can be sorted
1 2012-1
2 2012-2
3 2012-
4 2012-10
5 32012-11
6 2012-12
Try to use "proc sql" to sort variables in dataset and create a new macro variable "var_YEAR_MONTH".
proc sql noprint;
select "'"|| trim(left(YEAR_MONTH)) || "'" into : var_YEAR_MONTH separated by " "
from INJ_SUMMARY_2
order by no_YEAR_MONTH;
quit;
2.)
You can use substring or scan function to get only numbers in proc sql like:
order by int(input(scan(YEAR_MONTH,1,'-')||scan(YEAR_MONTH,2,'-'),8.));
Then put that variable in axis with order:
symbol1 i=join color=red v=dot;
symbol2 i=join ci=green v=square;
axis1 label=(a=90 'Sum');
axis2 label=(a=90 'Orders');
axis3 label=('Month') order=(&var_YEAR_MONTH) VALUE=(ANGLE=-45);
legend1 label=none;
title 'Multiple Plot Lines';
proc gbarline data=INJ_SUMMARY_2;
bar YEAR_MONTH / raxis=axis1 sumvar=VOLUME width=10 maxis=axis3;
plot / sumvar=ORDERS raxis=axis2;
run;quit;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.