Data visualization with SAS programming

Graph stacked bars ordered by 'segments' value /Base or Enterprise guide

Accepted Solution Solved
Reply
Contributor
Posts: 20
Accepted Solution

Graph stacked bars ordered by 'segments' value /Base or Enterprise guide

 

 

Good morning,

After searching and searching I´m unable to order the bars of a graph with SAS as I do with excel.

I need the bars ordered by the descending value of each segment of the bar (by month, showing in descending order those that have a higher value in the latest month, then those with higher value in the penultimate month etc.. ). At the moment is showing descending order by the total frequency (all segments, last month + penultimate month etc). I think it is much clearer looking at the graphs below.

Can any one help me please or if not possible with Base SAS or enterprise guide, can any other SAS module do it?

Thanks in advance.

 

 

 

 

 

 

SAS graph: bars ordered by total frequencySAS graph: bars ordered by total frequencyDesired: Excel graph ordered by the frequency for each month (segments of the bar)Desired: Excel graph ordered by the frequency for each month (segments of the bar)


Accepted Solutions
Solution
‎08-10-2017 03:03 AM
Super User
Posts: 11,343

Re: Graph stacked bars ordered by 'segments' value /Base or Enterprise guide

With the data in the order of the dataset provided this seems to do what is requested:

proc sgplot data=r3gg;
   vbar  vehiculo / response=numiiss group=var_mes grouporder=descending stat=sum;
   xaxis discreteorder=data;
run;

It is very likely that your default display area for the graph will be a bit cramped for the number of categories used.

 

You might add

ODS Graphics / width=20cm ;

or similar to have more width.

The colors will come from the current ODS style or provide additional information. This is a bare bones example and additional appearance options will be up to you.

Note the AXIS, SYMBOL and PATTERN statements of SAS/Graph will not have any effect on this plot.

View solution in original post


All Replies
Super User
Super User
Posts: 7,997

Re: Graph stacked bars ordered by 'segments' value /Base or Enterprise guide

Not really anything to go on here.  From what I can see is your values on the X-Axis are different, if thats it then just change your data to reflect what you want in the output.  I can't really say anything else as there is no Test data in the form of a datastep, or code etc.

You will find this page helpful for all graphs, best resource:

http://blogs.sas.com/content/graphicallyspeaking/

And a search shows a fair few examples:

http://blogs.sas.com/content/?s=stacked

Contributor
Posts: 20

Re: Graph stacked bars ordered by 'segments' value /Base or Enterprise guide

Thanks RW9,

I have been through the info you attach but I can´t see the same 'problem' I have.

The code below is from Enterprise Guide slightly modified. I also attach two examples that may be clearer. What I find is that even if my data  is ordered as I want in the data set (i.e. descending month (var_mes), descending value_per_month (numiiss), I loose this order when graphing or proc tabulating. (I already have seen this is not possible to do in proc tabulate, to order by the analysis variable). i also attach the data.

Many thanks in advance.

 

 

PROC SORT
	DATA=TMP00001.t4_acumuladoiissveh(KEEP=Vehiculo var_mes numiiss_Mean Tipo_Tren)
	OUT=WORK.SORTTempTableSorted
	;
	BY DESCENDING tipo_tren descending var_mes descending numiiss_mean;
RUN;
	PATTERN1 COLOR=CXCCFFCC;
    PATTERN2 COLOR=CXCCFFCC;
	PATTERN3 COLOR=CX99CC00;
	PATTERN4 COLOR=CXFFFF99;
	PATTERN5 COLOR=YELLOW;
	PATTERN6 COLOR=CXFF9900;
	PATTERN7 COLOR=RED;
	
	PATTERN8 COLOR = _STYLE_;
	PATTERN9 COLOR = _STYLE_;
	PATTERN10 COLOR = _STYLE_;
	PATTERN11 COLOR = _STYLE_;
	PATTERN12 COLOR = _STYLE_;
	PATTERN13 COLOR = _STYLE_;
Legend1
	FRAME
	label=("Mes")
	;
Axis1
	STYLE=1
	WIDTH=1
	MINOR=NONE
	label=("Número" justify=l "de Incidencias")


;
Axis2
	STYLE=1
	WIDTH=1


;
TITLE;
TITLE1 "Incidencias por vehículo y mes";
FOOTNOTE;
FOOTNOTE1 "";
PROC GCHART DATA=WORK.SORTTempTableSorted
;
	VBAR 
	 Vehiculo
 /
	SUMVAR=numiiss_Mean descending
	SUBGROUP=var_mes descending
	CLIPREF
FRAME discrete	TYPE=SUM
	LEGEND=LEGEND1
	DESCENDING
	COUTLINE=BLACK
	RAXIS=AXIS1
	MAXIS=AXIS2
;
	BY DESCENDING Tipo_Tren;
/* -------------------------------------------------------------------
   Fin de código de la tarea
   ------------------------------------------------------------------- */
RUN; QUIT;
%_eg_conditional_dropds(WORK.SORTTempTableSorted);
TITLE; FOOTNOTE;
PATTERN1;
PATTERN2;
PATTERN3;
PATTERN4;
PATTERN5;
PATTERN6;

sas output, it is ordered by total frequency but I would like the ones in red (latest month) to show in the first bars on the left and in order of the value (please see next graph)sas output, it is ordered by total frequency but I would like the ones in red (latest month) to show in the first bars on the left and in order of the value (please see next graph)Excel graph ordered by descending month (july, june, may etc..) and then first the value in july, then the value in june, then the value in may etc... so the ones with no data in the latests months show on the right of the graphExcel graph ordered by descending month (july, june, may etc..) and then first the value in july, then the value in june, then the value in may etc... so the ones with no data in the latests months show on the right of the graph

Attachment
Super User
Super User
Posts: 7,997

Re: Graph stacked bars ordered by 'segments' value /Base or Enterprise guide

Post a sample of your test data in the form of a datastep in the body of the post, using the code window - {i} above post area.  Only needs a few lines.

Basically to get the right order you need to set you data correctly.  If it is character varaibles, then use a number behind it, then format that rather than letting the procedure sort internally.  If this is an automatic calculation in the graph, then take it out of the graphs hands, do the calculation before that and then use that ini your graph rather than let the procedure handle it.  Also, move away from the ancient graphing procedures, SGPlot, and Graph Template Language should be used in all cases since version 9 at leats, it allow more flexibility.  Examples of the templates or sgplot code can be found in that link.  gchart however wont be as it decades out of date.

Solution
‎08-10-2017 03:03 AM
Super User
Posts: 11,343

Re: Graph stacked bars ordered by 'segments' value /Base or Enterprise guide

With the data in the order of the dataset provided this seems to do what is requested:

proc sgplot data=r3gg;
   vbar  vehiculo / response=numiiss group=var_mes grouporder=descending stat=sum;
   xaxis discreteorder=data;
run;

It is very likely that your default display area for the graph will be a bit cramped for the number of categories used.

 

You might add

ODS Graphics / width=20cm ;

or similar to have more width.

The colors will come from the current ODS style or provide additional information. This is a bare bones example and additional appearance options will be up to you.

Note the AXIS, SYMBOL and PATTERN statements of SAS/Graph will not have any effect on this plot.

Contributor
Posts: 20

Re: Graph stacked bars ordered by 'segments' value /Base or Enterprise guide

This is it. thanks very much Ballardw.

☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 151 views
  • 2 likes
  • 3 in conversation