proc import out=work.presidents datafile="/home/grego1/STAT 540/Presidents.xlsx" dbms=xlsx replace; sheet="Presidents"; run; data a; set presidents; *This statement creates a missing value for the last record; term_length=substr(term,6,4)-substr(term,1,4); length nterms $10; /* This IF statement simplifies the single term/multiple term categorization */ /* It also handles the missing value appropriately. */ if term_length le 4 then nterms='One Term'; else nterms='Multi-Term'; label nterms='Terms Served'; label age='Years'; run; /* Simple plot */ proc sgplot data=a; vbar party; run; /* Plot with grouping. The LABEL statement in the DATA step improves the legend */ proc sgplot data=a; hbar party/group=nterms; run; *Plot side-by-side--default for groupdisplay is STACK; proc sgplot data=a; hbar party/group=nterms groupdisplay=cluster; run; /* Plotting average age by party */ proc sgplot data=a; vbar party/response=Age stat=mean; title 'Average Inaugural Age of US Presidents'; run; title; /* Save two graphs to separate TIF files on your computer */ ods listing gpath='/home/grego1/STAT 540/'; ods graphics on/imagefmt=tiff; proc sgplot data=a; vbar party; run; /* Plot with grouping. The LABEL statement in the DATA step improves the legend */ proc sgplot data=a; hbar party/group=nterms groupdisplay=cluster; run; ods graphics off; *Restore default ods listing (though likely unnecessary); ods listing; ods html; /* Save graphs with better names */ ods listing gpath='/home/grego1/STAT 540'; ods graphics / reset imagename='PresidentialAffiliation' outputfmt=jpg height=7.5cm; proc sgplot data=a; vbar party; run; /* Plot with grouping. The LABEL statement in the DATA step improves the legend */ ods graphics / reset imagename='TermsAffiliation' outputfmt=jpg; proc sgplot data=a; hbar party/group=nterms groupdisplay=cluster; run; ods graphics off; ods listing;