I have plotted hbar chart with sgplot for different customers. The problem I have is if the plot has more than 1 bar the widths are okay but if the plot outputs only one bar the width becomes double. Is there any way I can make the width fixed no matter the number of bars created. I have already used the hbar statement barwidth=0.8 but this is not helping. I will appreciate any help. Thanks in advance
There is no way to set a fixed bar width that will apply to all cases. SG computes such things outside in, not inside out. When there is only one category, the computations are a bit different. We can look into it.
However, If you know you have a one category situation, you can try setting the offsetmin and offsetmax on the category axis to reduce the bar width. yaxis offsetmin=0.3 offsetmax=0.3;
Okay, thanks a lot. I will try that and let you know the outcome
I have the offsetmin and offsetmax set to 0.05 does that have any influence on the width of the bars?
yaxis display=(nolabel) offsetmin=0.05 offsetmax=0.05;
Hello Sanjay,
sorry I was on holidays. I came back today and tried your suggestion but it didn't work. Is there any other way I can adjust the width?
I will be glad if any one can help.
Am just thinking of using if statement. To say if number of bars less than 4 then width=? else width =?
but I haven't tried that yet.
If you share your program with sample data, it is easier for others to help. "Did not work" does not provide much information.
Hi Sanjay;
sorry for repling so late. I was a little bit busy with some other issues. Here is the code for Program. The problem is that the width decreases with the number of bars. I do not want that, I want the width to be fix no matter the number of bars. I hope you can help.
Thanks in advance
proc import datafile= "saslibrary\test1.xls" out=test
dbms=xls replace;
getnames=yes;
run;
%macro report ();
%do i = 501 %to 505 ;
proc sgplot data=test pad=(bottom=5%);
styleattrs datacolors=(red);
yaxis grid type=discrete discreteorder=data;
hbar sport_typ /response=count
barwidth=0.8 missing datalabel = count;
yaxis display=(nolabel) offsetmin=0.05 offsetmax=0.05;
xaxis label='Number of Athletes' grid offsetmin=0.0 offsetmax=0.05;
where rep_id = &i;
run;
title " ";
%end;
%mend report;
%report ();
A few things. Like in many other SAS programs, you do not need a macro. Sort your data by rep_id and use a BY statement in PROC SGPLOT instead as in this small example
proc sort data=sashelp.class;
by sex;
run;
proc sgplot data=sashelp.class;
by sex;
scatter x=height y=weight;
run;
I don't think it is possible to fix a bar width, so it does not change with the number of bars in your plot. As the documentation states, the BARWIDTH statement "specifies the width of the bars as a ratio of the maximum possible width".
ok thanks for your reply
Please see my previous answer to this question. "There is no way to set a fixed bar width that will apply to all cases. SG computes such things outside in, not inside out." For a set graph size, the bar width will depend on the number of bars in the graph. More bars -> less bar width.
okay Sanjay, thanks for that
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: