As the first step in the decommissioning of sasCommunity.org the site has been converted to read-only mode.


Here are some tips for How to share your SAS knowledge with your professional network.


Using the GPLOT Procedure: How Can I Have the Horizontal Axis Values Based on the Order in Which the Data are Read?

From sasCommunity
Jump to: navigation, search

Mike Zdeb

If you use PROC GPLOT to create a chart, SAS/Graph will produce an x-axis with values in ascending order. For example, the following SAS code is based on the SAS support web site example Sample 24916: Plot X axis values in data order.

data one;
input state $2. sales @@;
datalines;
VA  5200 SC  9800 NC  7500 GA  12500 NY  17600
;
run;
 
goptions reset=all ftext='calibri' htext=2 gunit=pct;
 
symbol1 i=none f='calibri' v='$' h=4 c=green;
 
axis1 order=(0 to 20000 by 5000) offset=(0,0) label=none;
axis2 minor=none offset=(3,3) label=none;
 
* place white space above the title using the LS option;
title1 h=4 'Sales by State --- SAS/Graph-Controlled Order on X-axis' ls=1;
 
* place white space on left and right of the graph;
title2 a=90 ls=1;
title3 a=-90 ls=1;
 
footnote1 'BASED ON:  http://support.sas.com/kb/24/916.html' ls=1;
footnote2 ls=1;
 
proc gplot data=one;
plot sales*state / vaxis=axis1 autovref haxis=axis2;
format sales dollar8.;
run;
quit;

To see the output from this procedure generated in V9.2 and Windows XP, click here.

The SAS/Graph default order on the x-axis can be overridden by using an ORDER option in the AXIS2 statement. If the desired order is that shown in the original data, the values can be hard-coded into the axis definition, matching the order in the original data.

axis2 minor=none offset=(3,3) label=none order=('VA' 'SC' 'NC' 'GA' 'NY');

However, rather than hard-coding the order, SAS can write the order option by using a macro variable that is created with PROC SQL.

data one;
input state $2. sales @@;
datalines;
VA  5200 SC  9800 NC  7500 GA  12500 NY  17600
;
run;
 
* place states (with quotes) in original order into a macro variable;
proc sql noprint;
select quote(state) into :states separated by ' ' from one;
quit;
 
goptions reset=all ftext='calibri' htext=2 gunit=pct;
 
symbol1 i=none f='calibri' v='$' h=4 c=green;
 
axis1 order=(0 to 20000 by 5000) offset=(0,0) label=none;
 
* use the macro variable to specify the order on the x-axis;
axis2 minor=none offset=(3,3) label=none order=(&states);
 
* place white space above the title using the LS option;
title1 h=4 'Sales by State --- User-Controlled Order on X-axis' ls=1;
 
* place white space on left and right of the graph;
title2 a=90 ls=1;
title3 a=-90 ls=1;
 
footnote1 'BASED ON:  http://support.sas.com/kb/24/916.html' ls=1;
footnote2 ls=1;
 
proc gplot data=one;
plot sales*state / vaxis=axis1 autovref haxis=axis2;
format sales dollar8.;
run;
quit;

To see the output from this procedure generated in V9.2 and Windows XP, click here.

If you have any questions about this posting, you are welcome to send me a note by clicking here ... email Mike. To see my other SAS Community postings click here.