SAS-sgplot-add label to bar clusters - sas

I am looking for an option to add label to bar clusters.
I want the values: 0.940, 0.250, 0.520, 0.580, and 0.230 in the middle of the clustered bars, like the following:

Assuming you have SAS 9.4 TS1M2 or later, you have an option, seglabel, which specifically does this, other than the 'display only once', which I don't think is a thing you can get out of the box.
proc sgplot data=sashelp.cars;
vbar cylinders/group=origin groupdisplay=cluster seglabel;
run;
If you don't, or you need more control than that gives you (such as your request to display only once), there are options, such as in my paper, Labelling without the hassle; it is for stacked bar charts, but the general approach would work for clustered also, you'd just have to adjust things some.
This gets somewhat close, and probably would get nearly perfect for your case; it needs some customization for groups that don't have all three group values present, but you don't have that in your example.
More complex solutions exist using annotations or GTL, as well, with this general approach (of overlaying on the precomputed bars a label).
proc summary data=sashelp.cars;
class origin cylinders;
types origin*cylinders;
var mpg_city;
output out=cars_summary(drop=_:) n(mpg_city)=count;
run;
data cars_labels;
set cars_summary;
if origin='Asia' then count_asia=count;
if origin='USA' then count_usa=count;
if origin='Europe' then count_eu=count;
ypos = floor(count/2);
run;
proc sgplot data=cars_labels;
vbarparm category=cylinders response=count/group=origin groupdisplay=cluster;
scatter x=cylinders y=ypos/markerchar=count_asia discreteoffset=-0.35;
scatter x=cylinders y=ypos/markerchar=count_usa discreteoffset=0.20;
scatter x=cylinders y=ypos/markerchar=count_eu discreteoffset=-0.1;
run;

Related

Trying to remove confidence bands and prediction bands from SAS REG procedure

My code
proc reg data=mydata plots=;
title "Correlation Between Estriol Levels and Birthweights";
model birthweight=estriol / clb cli clm;
run;
Plots the regression line and scatter plot but also includes the confidence bands and prediction bands. Is there a way I can hide one, the other, or both from the REG output?
I don't believe there's a way within PROC REG but you can accomplish something similar using PROC SGPLOT. See the documentation here for specifics such as modifying the colour/style or doing multiple lines.
Here's an example that uses the SASHELP.CLASS data set as an example.
proc sgplot data=sashelp.class;
reg x=age y=height;
run;
Documentation:
http://support.sas.com/documentation/cdl/en/grstatproc/69716/HTML/default/viewer.htm#p0mn6vl6clqbgyn1ivs69lezdxhf.htm
By using plots=; you are requesting the default set of plots in the output. You probably want to suppress the default set using plots(only)= and then specifying only the graphics you want.
See the PROC REG statement options at http://support.sas.com/documentation/cdl/en/statug/66859/HTML/default/viewer.htm#statug_reg_syntax01.htm

How do I create a pie chart in SAS for a variable whose values I've grouped using PROC FORMAT?

I have a variable ideology that takes on values from 1 to 7. I've decided to make these continuous values into three groups using PROC FORMAT like so:
proc format;
value ideofmt
1-2='Lib or Extr Lib'
3-5='Mod Lib, Slight Lib or Slight Cons'
6-7='Cons or Extr Cons';
run;
I want to create a pie chart that takes into account my grouping of these continuous values, if possible without having to modify the data itself. What I've tried is:
proc gchart data=sasuser.project2;
pie ideology /noheading percent=arrow slice=inside value=inside coutline=black
woutline=2;
format ideology ideofmt.;
run;
This gets me a pie chart that has the group labels that I want, but the pie chart is split into 7 slices (corresponding to the 7 values) instead of 3 (corresponding to the 3 groups).
Any help with this would be much appreciated. Thanks!
Add the discrete option to your pie statement:
proc gchart data=sasuser.project2;
pie ideology /noheading percent=arrow slice=inside value=inside coutline=black discrete
woutline=2;
format ideology ideofmt.;
run;
quit;
Otherwise, I think SAS assigns slices based on quantiles of your data, ignoring formatted values.
Also, a quit statement is required at the end of proc gchart in addition to the run statement.

how to find outliers in sas with proc means?

is there a way to detect an outlier from proc means while calculating min max Q1 and Q3?
the box plot procedure is not working on my SAS and I am trying to perform a boxplt in excel with the values from SAS.
Assuming you have a specific definition for what an outlier is, PROC UNIVARIATE can calculate the value that appears at that percentile using the PCTLPTS keyword on the OUTPUT statement. It also will identify extreme observations individually, so you can see the top few observations (if you have few enough observations that the number of extremes is likely to be <= 5).
The paper A SAS Application to Identify and Evaluate Outliers goes over a few of the ways you can look at outliers, including box plots and PROC UNIVARIATE, and includes some regression-based approaches as well.
If you want a 'standard boxplot' use the outbox= option in SAS to create the standard data set used for a box plot.
proc boxplot data=sashelp.class;
plot age*sex / outbox = xyz;
run;

Remove overlapping X-axis labels on a barchart

Short of using annotations, I have been unable to find a reasonable way to prevent my x-axis labels from overlapping when using a barchartparm in SAS. From the documentation, they clearly state that barcharts use a discrete axis and the other axis types such as time are not permissible for them. Although conceptually this makes sense it seems like an 'unnecessary' limitation to enforce as it leaves no control over the x-axis labeling as every discrete label will be printed.
Sample data:
data test;
format rpt_date date9.;
do rpt_date=date()-90 to date();
root = round(ranuni(1) *100,1);
output;
end;
run;
Define the chart template:
proc template;
define statgraph giddyup;
begingraph;
layout overlay;
barchartparm x=rpt_date y=root ;
endlayout;
endgraph;
end;
run;
Create the chart:
proc sgrender data=test template=giddyup;
run;
Result:
I tried to be duct-tape it and create a custom format for the x-axis that would 'blank-out' many of the values, and although the chart was produced, it stacked all the blanks together (??) and also produced a warning.
I've also tried using the alternate x2axisopts and setting the axis to secondary with no luck.
If I used a series chart I would be able to control the axis fine, but in my case the data is much easier to interpret as a barchart. Perhaps they needed to add additional options to the xaxisopts for barcharts.
The most frustrating thing here is that it's something that you can do in excel in 2 seconds, and to me seems like it would be a very common chart in excel, that is not easily reproducible in SAS!
EDIT: I also don't want to use proc gchart .
Ok now I feel silly. Turns out that histograms will achieve the same result nicely:
histogramparm x=rpt_date y=root ;
Still a valuable question I guess as I spent a lot of time googling for answers and could not find a solution.
Good thing I didn't want it horizontal...

Storing the PCT from proc gchart in a table

I am using proc chart on a discrete variable.
I am curious how to store just the percentage value (pct) from the output into a table ?
I tried the gout option (gout=test1) below but it stores the whole graph as an image,
I dont want that. I just want the pct values.
proc gchart data= Native5 gout=test1;
hbar RBMI/discrete;
run;
Well, since the GOUT option was intended to output graphics, it would be a surprise if it did anything else. Just for the sake of interest, what version of SAS are you using? In 9.2 the GOUT option appears to be deprecated. Also, PROC GCHART is subsumed into PROC SGPANEL.
Is there a particular reason you want the relative frequencies from GCHART as opposed to TABULATE or FREQ?