%let np=norow nocol nopercent; title 'ONE ZERO CELL, NO ZERO MARGINALS IN THE DATA SET'; proc freq data=sashelp.class; table sex*age / &np; run; *****************************************************; * CREATE A FORMAT CALLING FOR ZERO MARGINALS; * SEVERAL CATEGORIES HAVE VALUES THAT DO NOT EXIST IN SASHELP.CLASS; proc format; value $sex 'M'='Male' 'F'='Female' 'U'='Unknown' ; value age low-10 = '<11' 11-16='OK' 17-high='17+'; run; title 'NO DATA IN THE "OUTLIER" CATEGORIES'; proc freq data=sashelp.class; table sex*age / &np; format sex $sex. age age.; run; * PROC SUMMARY USED TO CREATE COUNTS; proc summary data=sashelp.class; class sex age; output out=x; format sex $sex. age age.; run; title 'STILL NO DATA IN THE "OUTLIER" CATEGORIES'; proc print data=x; run; * PROC SUMMARY USED TO CREATE COUNTS + PRELOADED FORMATS; proc summary data=sashelp.class completetypes nway; class sex age / preloadfmt; output out=x (drop=_type_); format sex $sex. age age.; run; title 'DATA IN THE "OUTLIER" CATEGORIES'; proc print data=x; * format _all_; run; title 'PROC FREQ USED TO "REARRANGE" PROC SUMMARY OUTPUT ... NO DATA IN OUTLIER CATEGORIES'; proc freq data=x; table sex*age / &np; weight _freq_; run; title 'PROC FREQ USED TO "REARRANGE" PROC SUMMARY OUTPUT ... ADD ZERO OPTION TO WEIGHT STATEMENT'; proc freq data=x; table sex*age / &np; weight _freq_ / zero; run; * PROC SUMMARY USED TO CREATE COUNTS + PRELOADED FORMATS + NO NWAY OPTION; proc summary data=sashelp.class completetypes; class sex age / preloadfmt; output out=x; format sex $sex. age age.; run; title 'DATA NOW INCLUDES MARGINAL TOTALS SEEN IN PROC FREQ'; proc print data=x; run; title 'PROC FREQ USING ONLY TABLE CELLS PRODUCED WITH PROC SUMMARY ... _TYPE_ = 3'; proc freq data=x; table sex*age / &np; weight _freq_ / zero; where _type_ eq 3; run; *****************************************************; title 'DATA IN SASHELP.CLASS'; proc freq data=sashelp.class; table height weight; run; * CREATE MORE CATEGORIES THAT SHOULD HAVE ZERO COUNTS; proc format; value $sex 'M'='Male' 'F'='Female' 'U'='Unknown' ; value age low-10='<11' 11-16='OK' 17-high='17+'; value weight low-<50='<50' 50-<80='50-<80' 80-<100='80-<100' 100-<150='100-<150' 150-high='150+'; value height low-<50='<50' 50-<80='50-<80' 80-high='80+'; run; title 'PROC SUMMARY + NO NWAY WITH THREE VARIABLES'; proc summary data=sashelp.class completetypes; class height sex age / preloadfmt; output out=x; format sex $sex. age age. height height.; run; * take a look at the output from PROC SUMMARY; * there are multiple set of counts; proc print data=x; run; * reduce the size, make it easier to use; proc format; value $ch ' '=' ' other='*'; value nm low-high='*' other=' '; run; title "PROC SUMMARY DATA SET WITH 3 VARIABLES (2**3 = 8 CATEGORIES)"; proc freq data=x; tables _type_*height*sex*age / list missing nocum nopercent; format _character_ $ch. _numeric_ nm. _type_ 2.; run; title; title 'PART OF THE PROC SUMMARY OUTPUT BY USING A WHERE STATEMENT'; proc freq data=x; table sex*age / &np; weight _freq_ / zero; where _type_ eq 3; run; title 'PART OF THE PROC SUMMARY OUTPUT BY USING A WHERE STATEMENT + THREE VARIABLES'; proc freq data=x; table height*sex*age / &np; weight _freq_ / zero; where _type_ eq 7; run; title 'USE CROSSLIST (LOOKS A BIT LIKE PROC SUMMARY OUTPUT)'; proc freq data=x; table height*sex*age / &np crosslist; weight _freq_ / zero; where _type_ eq 7; run; *****************************************************; proc summary data=sashelp.class completetypes; class weight height sex age / preloadfmt; output out=x; format sex $sex. age age. height height. weight weight.; run; title 'PROC SUMMARY OUTPUT + NO NWAY OPTION + FOUR VARIABLES'; proc print data=x; run; title "PROC SUMMARY DATA SET WITH 4 VARIABLES (2**4 = 16 CATEGORIES)"; proc freq data=x; tables _type_*weight*height*sex*age / list missing nocum nopercent; format _character_ $ch. _numeric_ nm. _type_ 2.; run; title; proc freq data=x; table sex*age / &np; weight _freq_ / zero; where _type_ eq 3; run; proc freq data=x; table weight*height / &np; weight _freq_ / zero; where _type_ eq 12; run; *****************************************************; * ZERO, SPARSE, NOSPARSE; proc freq data=x; table height*sex*age / list &np; weight _freq_ / zero; where _type_ eq 7; run; proc freq data=sashelp.class; table sex*age / &np; run; proc freq data=sashelp.class; table sex*age / list &np; run; proc freq data=sashelp.class; table sex*age / list &np sparse; run; *****************************************************; * MISSING and MISSPRINT; data class; set sashelp.class; if ranuni(0) lt .2 then call missing(age, sex); /*if ranuni(0) lt .2 then age = .;*/ /*else*/ /*if ranuni(0) lt .3 then sex = ' ';*/ run; proc print data=class; run; proc freq data=class; table sex*age / &np; table sex*age / missing; table sex*age / missprint; run; *****************************************************; * USING COMPLETETYPES IN PROC SUMMARY WITHOUT A PRELOADED FORMAT; data x; input v1 :$1. v2 v3 :$1. v4; datalines; a 1 x 77 b 2 y 88 c 3 z 99 ; title 'DATA SET X'; proc print data=x; run; proc summary data=x nway completetypes; class v1 v2 v3; var v4; output out=y (drop=_:) sum=; run; title 'ALL COMBINATIONS OF THE THREE VARIABLES'; proc print data=y; run; * if you want to ... replace missing values of V4 with zeroes; proc stdize data=y out=y reponly missing=0; var v4; run; proc print data=y; run;