/* Example from the online SAS Documentation at support.sas.com */ /* Example 1: WHEN THE DATA SET ALREADY CONSISTS OF COUNTS */ data Color; input Region Eyes $ Hair $ Count @@; label Eyes ='Eye Color' Hair ='Hair Color' Region='Geographic Region'; datalines; 1 blue fair 23 1 blue red 7 1 blue medium 24 1 blue dark 11 1 green fair 19 1 green red 7 1 green medium 18 1 green dark 14 1 brown fair 34 1 brown red 5 1 brown medium 41 1 brown dark 40 1 brown black 3 2 blue fair 46 2 blue red 21 2 blue medium 44 2 blue dark 40 2 blue black 6 2 green fair 50 2 green red 31 2 green medium 37 2 green dark 23 2 brown fair 56 2 brown red 42 2 brown medium 53 2 brown dark 54 2 brown black 13 ; PROC FREQ DATA = color; tables region; weight Count; title 'Number of Children per Region'; RUN; PROC FREQ DATA = color; tables hair / out=freqhair; weight Count; title 'Number of Children per Hair Color'; RUN; PROC FREQ DATA = color; tables eyes / out=freqeye; weight Count; title 'Number of Children per Eye Color'; RUN; PROC FREQ DATA = color; tables hair * eyes; weight Count; title 'Table of hair color and eye color for Sample of European Children'; RUN; PROC FREQ DATA = color; tables region*hair*eyes / norow nocol; weight Count; title 'Table of hair color and eye color (separate for Region of Europe)'; RUN; /* Sorting to print hair color, ranked by frequency */ proc sort data = freqhair out=sorthair; by descending count; run; proc print data=sorthair; title 'Hair Colors ranked from most frequent to least'; run; /* Sorting to print eye color, ranked by reverse frequency */ proc sort data = freqeye out=sorteye; by count; run; proc print data=sorteye; title 'Eye Colors ranked from least frequent to most'; run; /* Example 2: WHEN THE DATA SET JUST CONTAINS THE OBSERVATIONS, NOT THE COUNTS */ /* What if the observations in the data set do not summarize by counts already? */ /* Just leave off the WEIGHT statement. */ /* A hypothetical student data set with categorical variables: */ DATA students; INPUT Name :$9. schoolclass :$9. gender $ prev_attempts; cards; Andy Freshman M 0 Beth Sophomore F 0 Cassandra Junior F 0 David Senior M 1 Edward Graduate M 0 Fabio Freshman M 0 George Junior M 2 Harriet Freshman F 0 Imelda Sophomore F 1 Juan Freshman M 0 Katrina Freshman F 0 Lucy Senior F 2 Miguel Freshman M 0 Nancy Sophomore F 0 Oscar Graduate M 0 Perry Sophomore M 1 Quinn Graduate M 0 Roger Senior M 1 Sally Junior F 0 Tom Senior M 1 Ursula Senior F 2 Vera Freshman F 0 Waldo Sophomore M 1 Xing Graduate F 0 Yao Freshman M 0 Zhimin Junior M 1 ; run; PROC PRINT data=students; run; PROC FREQ data=students; tables schoolclass; title 'Counts per Class'; RUN; PROC FREQ DATA = students; tables schoolclass*prev_attempts; title 'Table of class and previous attempts'; RUN; PROC FREQ DATA = students; tables gender*schoolclass*prev_attempts / norow nocol nopercent; title 'Table of class and previous attempts, separate by gender'; RUN;