BookmarkSubscribeRSS Feed
Anita_n
Pyrite | Level 9

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

9 REPLIES 9
Jay54
Meteorite | Level 14

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;

Anita_n
Pyrite | Level 9

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;

 

Anita_n
Pyrite | Level 9

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.

Jay54
Meteorite | Level 14

If you share your program with sample data, it is easier for others to help.  "Did not work" does not provide much information.

Anita_n
Pyrite | Level 9

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 ();
PeterClemmensen
Tourmaline | Level 20

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".

 

Anita_n
Pyrite | Level 9

ok thanks for your reply

Jay54
Meteorite | Level 14

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.

Anita_n
Pyrite | Level 9

okay Sanjay, thanks for that

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

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

View all other training opportunities.

Discussion stats
  • 9 replies
  • 4235 views
  • 0 likes
  • 3 in conversation