Data visualization with SAS programming

SAS - how to sort text within PROC GBARLINE graph

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 131
Accepted Solution

SAS - how to sort text within PROC GBARLINE graph

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


Accepted Solutions
Solution
‎12-05-2012 01:15 PM
Super User
Posts: 19,105

Re: SAS - how to sort text within PROC GBARLINE graph

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.

View solution in original post


All Replies
Solution
‎12-05-2012 01:15 PM
Super User
Posts: 19,105

Re: SAS - how to sort text within PROC GBARLINE graph

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.

Super Contributor
Posts: 1,636

Re: SAS - how to sort text within PROC GBARLINE graph

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

SAS Super FREQ
Posts: 1,139

Re: SAS - how to sort text within PROC GBARLINE graph

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.

N/A
Posts: 1

Re: SAS - how to sort text within PROC GBARLINE graph

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;

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 534 views
  • 0 likes
  • 5 in conversation