Help using Base SAS procedures

Am I using the right using proc sort or by function?

Reply
N/A
Posts: 0

Am I using the right using proc sort or by function?

Hi Guys,

I am a newly minted stat masters student and am using SAS for the first time in my life. I do need some help with some SAS coding to get what my professor wants.

I will post the code here:

data Police;
input County $ 1-10 Officers Assaults;
datalines;
Atlantic 961 143
Bergen 2177 213
Burlington 786 109
Camden 1360 199
Cape May 343 36
Cumberland 291 52
Essex 2833 429
Gloucester 583 113
Hudson 1940 323
Hunterdon 182 8
Mercer 929 155
Middlesex 1602 158
Momouth 1499 177
Morris 1080 75
Ocean 1064 134
Passaic 1126 202
Salem 125 19
Somerset 603 51
Sussex 201 31
Union 1438 275
Warren 153 12
;

proc print noobs data=Police;
var County Officers Assaults;
title Assaults on New Jersey Police Officers;
run;

proc sgplot data=Police;
xaxis label="County";
yaxis label="Count";
vbar County/ response=Officers
barwidth=0.5
transparency=0.2;
vbar County/ response=Assaults;
run;


I would like for my vbar chart to be sorted in descending order using Officers as the variable. I tried using proc sort under data but it did not work for vbar. Alternatively, I tried sorting using a by statement in proc sgplot and what I got in return were 21 singular charts. I am a little stumped as to what I am doing wrong.

Can anyone help? Any help is very much appreciated!

Henry
SAS Super FREQ
Posts: 8,744

Re: Am I using the right using proc sort or by function?

Hi:
You said: "I tried using proc sort under data but it did not work for vbar. Alternatively, I tried sorting using a by statement in proc sgplot and what I got in return were 21 singular charts." Your instinct to do a sort was correct, but your approach -- to use a "BY statement in PROC SGPLOT" or a "PROC SORT under DATA" -- was flawed.

Using a BY statement in any procedure or DATA step does not actually PERFORM the sort -- it only uses the sorted data -- generally, as you saw, to treat each BY group as a unit to be handled separately -- such as when you got 21 separate charts. I'm not sure what you mean by saying you tried PROC SORT "under" a DATA I suspect you mean that you added a PROC SORT in the code after your DATA step where you created the POLICE dataset -- this was CORRECT, if you had this:
[pre]
proc sort data=police;
by descending Officers;
run;
[/pre]

which produces a sorted version of the POLICE dataset with the rows organized in descending order based on the value of the OFFICERS variable.

The next thing would be to look up the XAXIS statement for PROC SGPLOT -- you can control the order of the XAXIS or YAXIS (from the default) by using options on the XAXIS or YAXIS statement. The option you would be interested in for the XAXIS would be the DISCRETEORDER= option (since the XAXIS is a "discrete" axis with one bar for every value of the character variable COUNTY).

When I do transparent overlays, I like to make the "underneath" bar a little bigger and make the "on top" bar the transparent bar, as shown below.

cynthia

ps -- in the future, you might want to post SGPLOT questions in the ODS GRAPHICS and SAS/GRAPH forum
[pre]
proc sgplot data=Police;
xaxis label="County" discreteorder=data;
yaxis label="Count";

vbar County/ response=Officers
barwidth=0.75;

vbar County/ response=Assaults
barwidth=.5
transparency=0.2;
run;
[/pre]
N/A
Posts: 0

Re: Am I using the right using proc sort or by function?

Thank you so much Cynthia for your help, will try to fix my situation.
Ask a Question
Discussion stats
  • 2 replies
  • 92 views
  • 0 likes
  • 2 in conversation