Creating Simple Graphics Using SAS - Sgplot Sgscatter Sgpanel
Creating Simple Graphics Using SAS - Sgplot Sgscatter Sgpanel
This handout covers the use of SAS procedures to get simple descriptive
statistics and create basic graphs. The procedures introduced are:
Proc Sgplot
Proc Sgpanel
Proc Sgscatter
The procedures demonstrated in this handout are new to SAS 9.2, and are
not available in previous releases. Check the SAS online documentation for
more information. The files produced are .png (portable network graphics)
files, which can be easily imported into other applications.
http://www.umich.edu/~kwelch/510/2009/handouts_2009.html
Save this data set in a folder on your desktop (or any other location
you choose). Do not double-click to open it.
Submit a libname statement to point to the folder (not the actual file)
where you have saved the data set. The libname statement only needs
to be submitted once when you start SAS.
Check the log to be sure the library (libref) was successfully assigned:
libname b510 "c:\documents and settings\kwelch\desktop\b510" ;
NOTE: Libref B510 was successfully assigned as follows:
Engine: V9
Physical Name: c:\documents and settings\kwelch\desktop\b510
1
Statistical Graphics Examples:
Boxplots:
title "Boxplot";
title2 "No Categories";
proc sgplot data=b510.employee;
vbox salary;
run;
title "Boxplot";
title2 "Category=Gender";
proc sgplot data=b510.employee;
vbox salary/ category=gender;
run;
2
title "Boxplot with Panels";
proc sgpanel data=b510.employee;
panelby jobcat / rows=1 columns=3 ;
vbox salary / category= gender;
run;
Barcharts:
3
title "Vertical Bar Chart";
title2 "Grouped by Gender";
proc sgplot data=b510.employee;
vbar jobcat /group=Gender;
run;
4
title "BarChart Paneled by Gender";
proc sgpanel data=b510.employee;
panelby gender ;
vbar jobcat / response=salary limitstat = stddev
limits = upper stat=mean;
run;
Histograms:
title "Histogram";
proc sgplot data=b510.employee;
histogram salary ;
run;
5
proc sgplot data=b510.employee;
histogram salary ;
density salary;
density salary / type=kernel;
run;
6
/*Create New Variables for Overlay*/
data employee2;
set b510.employee;
if gender = "m" then salary_m = salary;
if gender = "f" then salary_f = salary;
run;
7
Scatterplots:
title "Scatterplot";
proc sgplot data=b510.employee;
scatter x=salbegin y=salary / group=gender ;
run;
8
title "Scatterplot Panels. Males only";
proc sgpanel data=b510.employee;
panelby gender;
scatter x=jobtime y=salary / group=jobcat;
loess x=jobtime y=salary ;
run;
9
title "Scatterplot Matrix";
title2 "Clerical Employees";
proc sgscatter data=b510.employee;
where jobcat=1;
matrix salbegin salary jobtime prevexp / group=gender;
run;
10
Using formats to make graphs more readable:
This example creates two formats, the first one is numeric (i.e. it can be
used to format a numeric variable), and the second one is character (i.e., it
can be used to format a character variable). These are temporary formats
and must be submitted each time you run SAS. Use a format statement to
apply the formats for each proc that you run, where appropriate.
proc format;
value jobcat 1="Clerical"
2="Custodial"
3="Manager";
value $Gender "f"="Female"
"m"="Male";
run;
11