BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
tparvaiz
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

4 REPLIES 4
Reeza
Super User

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.

Linlin
Lapis Lazuli | Level 10

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

Jay54
Meteorite | Level 14

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.

beni75
Calcite | Level 5

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;

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

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

View all other training opportunities.

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