/* Import the Excel spreadsheet into the WORK directory as data set "presidents" */ proc import out=work.presidents datafile="/home/grego1/STAT 540/Presidents.xlsx" dbms=xlsx replace; sheet="Presidents"; run; data a; set presidents; term_length=substr(term,6,4)-substr(term,1,4); length nterms $10; /* This IF statement simplifies the single term/multiple term categorization */ if term_length le 4 then nterms='One Term'; else nterms='Multi-Term'; label nterms='Terms Served'; label age='Years'; run; /* Simple histogram. Change scale from percentage to count and center the x-axis labels*/ ods listing; title; proc sgplot data=a; title 'Age of US Presidents at Inauguration'; histogram age; run; /* Change some of the defaults */ proc sgplot data=a; title 'Age of US Presidents at Inauguration'; histogram age/scale=count showbins fillattrs=(color=red); run; /* It might be better to remove showbins--I don't like the fractional age labels. */ /* Change the color of the bars to red */ proc sgplot data=a; title 'Age of US Presidents at Inauguration'; histogram age/scale=count fillattrs=(color=red); run; /* Remove the fill color */ proc sgplot data=a; title 'Age of US Presidents at Inauguration'; histogram age/scale=count nofill; run; /* Add kernel density estimate */ proc sgplot data=a; title 'Age of US Presidents at Inauguration'; histogram age/scale=count nofill; density age/type=kernel; run; /* Fine tune the amount of smoothing and improve the legend--this is slightly over-smoothed */ proc sgplot data=a; title 'Age of US Presidents at Inauguration'; histogram age/scale=count nofill; density age/type=kernel(c=2); run; proc sgplot data=a; title 'Age of US Presidents at Inauguration'; histogram age/scale=count nofill; density age/type=kernel(c=1.5); run; /* Box plot of age by party--with outlier labels */ proc sgplot data=a; title 'Age of US Presidents at Inauguration by Political Affiliation'; vbox age/category=party datalabel=name; run; proc sgplot data=a; where party in ("Republican" "Whig" "Democratic"); title 'Age of US Presidents at Inauguration by Political Affiliation'; vbox age/category=party datalabel=name; run; *Add grouping variable (relatively new feature); proc sgplot data=a; where party in ("Republican" "Whig" "Democratic"); title 'Age of US Presidents at Inauguration by Political Affiliation'; vbox age/category=party datalabel=name group=nterms; run;