/***************************************** SAS 9.2 Statistical Graphics Examples filename: sgraphics.sas ******************************************/ /*Option to make the output look nice, if you use Courier or another non-SAS font*/ OPTIONS FORMCHAR="|----|+|---+=|-/\<>*"; /*Enable the ODS Graphics Editor*/ ods listing sge = on; /************************************************ Change the path to sasdata2 to the folder where you have stored your SAS datasets, Employee.sas7bdat, and Autism.sas7bdat *************************************************/ libname sasdata2 "C:\Users\kwelch\Desktop\sasdata2"; /*Boxplots*/ title "Boxplot"; title2 "No Categories"; proc sgplot data=sasdata2.employee; vbox salary; run; title "Boxplot"; title2 "Category=Gender"; proc sgplot data=sasdata2.employee; vbox salary/ category=gender; run; title "Boxplot with Panels"; proc sgpanel data=sasdata2.employee; panelby jobcat / rows=1 columns=3 ; vbox salary / category= gender; run; /*Barcharts*/ title "Vertical Bar Chart"; proc sgplot data=sasdata2.employee; vbar jobcat ; run; title "Vertical Bar Chart"; title2 "Grouped by Gender"; proc sgplot data=sasdata2.employee; vbar jobcat /group=Gender; run; title "BarChart with Mean and Standard Deviation"; proc sgplot data=sasdata2.employee; vbar jobcat / response=salary limitstat = stddev limits = upper stat=mean; run; data afifi; set sasdata2.afifi; if survive=1 then died=0; if survive=3 then died=1; if shoktype=2 then shock=0; if shoktype >2 then shock=1; run; proc format; value shokfmt 2="Non-Shock" 3="Hypovolemic" 4="Cardiogenic" 5="Bacterial" 6="Neurogenic" 7="Other"; run; title "Barchart of Proportion Died for each Shock Type"; proc sgplot data=afifi; vbar shoktype / response=died stat=mean; format shoktype shokfmt.; run; title "BarChart Paneled by Gender"; proc sgpanel data=sasdata2.employee; panelby gender ; vbar jobcat / response=salary limitstat = stddev limits = upper stat=mean; run; /*Histograms*/ title "Histogram"; proc sgplot data=sasdata2.employee; histogram salary ; run; title "Histogram With Density Overlaid"; proc sgplot data=sasdata2.employee; histogram salary ; density salary; density salary / type=kernel; keylegend / location = inside position = topright; format salary dollar10.; run; title "Histogram with Panels"; title2 "Exclude Custodial"; proc sgpanel data=sasdata2.employee; where jobcat not=2; panelby gender jobcat/ rows=2 columns = 2; histogram salary / scale=proportion; run; /*use scale=proportion, count, or percent(default)*/ title "Overlay different variables"; proc sgplot data=sasdata2.employee; histogram salbegin ; histogram salary / transparency = .5; run; /*Create New Variables for Overlay*/ data employee2; set sasdata2.employee; if gender = "m" then salary_m = salary; if gender = "f" then salary_f = salary; run; title "Overlaid histograms"; title2 "Same variable, but two groups "; proc sgplot data=employee2; histogram salary_m; histogram salary_f / transparency=0; run; /*Scatterplots*/ title "Scatterplot"; proc sgplot data=sasdata2.employee; scatter x=salbegin y=salary / group=gender ; run; title "Scatterplot"; proc sgplot data=sasdata2.employee; scatter x=salbegin y=salary / group=gender ; ellipse x=salbegin y=salary / type=predicted alpha=.05; run; title "Scatterplot with Regression Line"; title2 "Clerical Only"; proc sgplot data=sasdata2.employee; where jobcat=1; scatter x=prevexp y=salary / group=gender ; reg x=prevexp y=salary / nomarkers cli clm; run; title "Scatterplot with Regression Line"; title2 "Separate Lines for Females and Males"; proc sgplot data=sasdata2.employee; where jobcat=1; reg x=prevexp y=salary / group=gender; run; title "Scatterplot Panels"; title2 "Loess Fit"; proc sgpanel data=sasdata2.employee; panelby jobcat / columns=3 rows=1; scatter x=jobtime y=salary / group=gender; loess x=jobtime y=salary /nomarkers; run; /*Scatterplot matrix*/ title "Scatterplot Matrix"; title2 "Clerical Employees"; proc sgscatter data=sasdata2.employee; where jobcat=1; matrix salbegin salary jobtime prevexp / group=gender diagonal=(histogram kernel); run; /*Series Plots*/ PROC IMPORT OUT= WORK.autism DATAFILE= "autism.csv" DBMS=CSV REPLACE; GETNAMES=YES; DATAROW=2; RUN; title "Spaghetti Plots for Each Child"; proc sgpanel data=autism; panelby sicdegp /columns=3; series x=age y=vsae / group=Childid markers legendlabel=" " lineattrs=(pattern=1 color=black); run; /*Overlay means on plot*/ proc sort data=autism; by sicdegp age; run; proc means data=autism noprint; by sicdegp age; output out=meandat mean(VSAE)=mean_VSAE; run; data autism2; merge autism meandat(drop=_type_ _freq_); by sicdegp age; run; title "Means Plots Overlaid on Data"; proc sgplot data=autism2; series x=age y=mean_VSAE / group=SICDEGP; scatter x=age y=VSAE ; run; /*Using Formats to make graphs more readable*/ proc format; value jobcat 1="Clerical" 2="Custodial" 3="Manager"; value $Gender "f"="Female" "m"="Male"; run; ods listing style=journal; title "Boxplot with Panels"; proc sgpanel data=sasdata2.employee; panelby jobcat / rows=1 columns=3 novarname; vbox salary / category= gender ; format gender $gender. jobcat jobcat.; run; /*Creating pdf output*/ ods pdf style=journal2; ods pdf file = "testing.pdf"; ods listing close; title "PDF Output"; proc sgpanel data=sasdata2.employee; panelby jobcat / columns=3 novarname; scatter x=jobtime y=salary / group=gender; loess x=jobtime y=salary/ nomarkers; format jobcat jobcat.; run; ods pdf close; ods listing; /*Creating HTML output*/ ods html style=statistical; ods html file = "testing.html"; ods listing close; title "HTML Output"; proc sgpanel data=sasdata2.employee; panelby jobcat / columns=3 novarname; scatter x=jobtime y=salary / group=gender; loess x=jobtime y=salary/ nomarkers; format jobcat jobcat.; run; ods html close; ods listing;