BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
EmilyAV
Calcite | Level 5

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.

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 order=ascending categoryorder=respasc groupdisplay=stack ;
xaxis display = none;
run;
 
This generates the following plot;
Capture.JPG
I am happy with all parts of this plot EXCEPT the displayed order of the groups in the bars (gender), which I want to be uniform (e.g. red always under blue).
Changing the sorting variable in proc sort and grouporder in proc sgplot do not seem to change the these displays.
 
What else should I try?
Thank you.
1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

 

/*
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;

Ksharp_0-1697455819839.png

 

 

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

Try the GROUPORDER= option

 

 

--
Paige Miller
EmilyAV
Calcite | Level 5

Thanks, @PaigeMiller.

Adding the GROUPORDER option doesn't generate an error message but also doesn't change the display.

PaigeMiller
Diamond | Level 26

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;
--
Paige Miller
PaigeMiller
Diamond | Level 26

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;
--
Paige Miller
EmilyAV
Calcite | Level 5

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:

 

...Capture2.JPG

Rick_SAS
SAS Super FREQ

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.

Ksharp
Super User

 

/*
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;

Ksharp_0-1697455819839.png

 

 

EmilyAV
Calcite | Level 5

Thank you. 

SAS Innovate 2025: Call for Content

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!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 8 replies
  • 2418 views
  • 5 likes
  • 4 in conversation