I have a simple dataset structured as follows:
data have;
input subject gender $ state $;
datalines
1 'M' 'TX'
2 'F' 'TX'
3 'M' 'TX'
4 'F' 'VA'
5 'F' 'VA'
...
I have generated some summary data using PROC FREQ and wish to plot it.
/*
order your freq in dataset before PROC SGPLOT
and switch it into VBARPARM .
*/
data fake;
call streaminit(123);
do state='AL','AK','AZ','AR','CA','CO','CT','DE','FL','GA','HI','ID','IL','IN','IA','KS',
'KY','LA','ME','MA','MD','MI','MN';
n=rand('integer',10,100);
do repl=1 to n;
if rand('uniform')<0.2 then gender='F'; else gender='M';
if state in ('ME' 'CO' 'DE') then gender=ifc(gender='F','M','F');
output;
end;
end;
drop n repl;
run;
proc sql;
create table counts as
select * ,sum(count) as total from
(select state,gender,count(*) as count from fake group by state,gender)
group by state
order by total
;
quit;
proc sgplot data=counts;
vbarparm category=state response=count/ group=gender groupdisplay=stack grouporder=ASCENDING ;
/*xaxis display = none;*/
run;
Thanks, @PaigeMiller.
Adding the GROUPORDER option doesn't generate an error message but also doesn't change the display.
With your very tiny example data set (which I have expanded), I cannot reproduce this problem.
data have;
input subject gender $ state $;
datalines;
1 M TX
2 F TX
3 M TX
4 F VA
5 F VA
1 M TX
2 F TX
3 M TX
4 F VA
5 F VA
1 M OR
2 F TX
3 M OR
4 M OR
5 M OR
1 M NY
2 F NY
3 M NY
4 F NJ
5 F NJ
;
proc freq data=have;
tables gender*state / out=counts outpct;
run;
proc sort data=counts ;
by gender ;
run;
proc sgplot data=counts;
vbar state / response=count group=gender groupdisplay=stack ;
xaxis display = none;
run;
Even when I produce a much larger fake data set, I cannot reproduce this problem.
data fake;
do state='AL','AK','AZ','AR','CA','CO','CT''DE','FL','GA','HI','ID','IL','IN','IA','KS',
'KY','LA','ME','MA','MD','MI','MN';
n=rand('integer',10,100);
do repl=1 to n;
if rand('uniform')<0.2 then gender='F'; else gender='M';
output;
end;
end;
drop n repl;
run;
proc freq data=fake;
tables gender*state / out=counts outpct;
run;
proc sort data=counts ;
by gender ;
run;
proc sgplot data=counts;
vbar state / response=count group=gender groupdisplay=stack ;
xaxis display = none;
run;
That's strange. Must have to do with the structure of the data?
I eliminated the possibility that the problem was a lack of zero counts in some groups, but still run into the same issue.
Not sure if this makes sense, but when I remove the categoryorder=respasc statement, the plot changes like this:
...
The documentation for the CATEGORYORDER=RESPASC option states
When a group variable is used with the CATEGORYORDER= option, the category order is not affected by the value of the groups. The categories are always sorted by the response statistic at a category level. [Emphasis added] |
When this option and the GROUPORDER= option are both specified, the GROUPORDER= option is ignored. |
I think you will need to sort the data by total count, then by gender. Then plot the data and use GROUPORDER=DATA to order the states.
/*
order your freq in dataset before PROC SGPLOT
and switch it into VBARPARM .
*/
data fake;
call streaminit(123);
do state='AL','AK','AZ','AR','CA','CO','CT','DE','FL','GA','HI','ID','IL','IN','IA','KS',
'KY','LA','ME','MA','MD','MI','MN';
n=rand('integer',10,100);
do repl=1 to n;
if rand('uniform')<0.2 then gender='F'; else gender='M';
if state in ('ME' 'CO' 'DE') then gender=ifc(gender='F','M','F');
output;
end;
end;
drop n repl;
run;
proc sql;
create table counts as
select * ,sum(count) as total from
(select state,gender,count(*) as count from fake group by state,gender)
group by state
order by total
;
quit;
proc sgplot data=counts;
vbarparm category=state response=count/ group=gender groupdisplay=stack grouporder=ASCENDING ;
/*xaxis display = none;*/
run;
Thank you.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.