options ls=80 noovp nodate nonumber; proc format ; value ss 1='female' 2='male'; value ee 1='hs' 2='coll' 3='grad'; value dd 1='coffee' 2='tea'; data sim; do i=1 to 1000; sex = int(2*ranuni(1)) + 1; edu = int(3*ranuni(1)) + 1; drink = rantbl(0,.7,.3); income = (10 + 20 * ranuni(1)) * 1000; output; end; format sex ss. edu ee. drink dd.; run; title 'Simple Two-way proc freq'; proc freq data=sim; tables sex*drink; run; Simple Two-way proc freq TABLE OF SEX BY DRINK SEX DRINK Frequency| Percent | Row Pct | Col Pct |coffee |tea | Total ---------+--------+--------+ female | 335 | 145 | 480 | 33.50 | 14.50 | 48.00 | 69.79 | 30.21 | | 47.25 | 49.83 | ---------+--------+--------+ male | 374 | 146 | 520 | 37.40 | 14.60 | 52.00 | 71.92 | 28.08 | | 52.75 | 50.17 | ---------+--------+--------+ Total 709 291 1000 70.90 29.10 100.00 title 'Three-way proc freq (multipage)'; proc freq data=sim; tables sex*drink*edu; run; Three-way proc freq (multipage) TABLE 1 OF DRINK BY EDU CONTROLLING FOR SEX=female DRINK EDU Frequency| Percent | Row Pct | Col Pct |hs |coll |grad | Total ---------+--------+--------+--------+ coffee | 100 | 121 | 114 | 335 | 20.83 | 25.21 | 23.75 | 69.79 | 29.85 | 36.12 | 34.03 | | 64.52 | 72.02 | 72.61 | ---------+--------+--------+--------+ tea | 55 | 47 | 43 | 145 | 11.46 | 9.79 | 8.96 | 30.21 | 37.93 | 32.41 | 29.66 | | 35.48 | 27.98 | 27.39 | ---------+--------+--------+--------+ Total 155 168 157 480 32.29 35.00 32.71 100.00 Three-way proc freq (multipage) TABLE 2 OF DRINK BY EDU CONTROLLING FOR SEX=male DRINK EDU Frequency| Percent | Row Pct | Col Pct |hs |coll |grad | Total ---------+--------+--------+--------+ coffee | 125 | 122 | 127 | 374 | 24.04 | 23.46 | 24.42 | 71.92 | 33.42 | 32.62 | 33.96 | | 71.43 | 67.78 | 76.97 | ---------+--------+--------+--------+ tea | 50 | 58 | 38 | 146 | 9.62 | 11.15 | 7.31 | 28.08 | 34.25 | 39.73 | 26.03 | | 28.57 | 32.22 | 23.03 | ---------+--------+--------+--------+ Total 175 180 165 520 33.65 34.62 31.73 100.00 title 'Three-way proc freq (list format)'; proc freq data=sim; tables sex*drink*edu/list; run; Three-way proc freq (list format) Cumulative Cumulative SEX DRINK EDU Frequency Percent Frequency Percent ----------------------------------------------------------------- female coffee hs 100 10.0 100 10.0 female coffee coll 121 12.1 221 22.1 female coffee grad 114 11.4 335 33.5 female tea hs 55 5.5 390 39.0 female tea coll 47 4.7 437 43.7 female tea grad 43 4.3 480 48.0 male coffee hs 125 12.5 605 60.5 male coffee coll 122 12.2 727 72.7 male coffee grad 127 12.7 854 85.4 male tea hs 50 5.0 904 90.4 male tea coll 58 5.8 962 96.2 male tea grad 38 3.8 1000 100.0 proc freq data=sim; tables sex*drink*edu/noprint out=next; run; title 'Three-way proc freq (output data set)'; proc print data=next; run; Three-way proc freq (output data set) OBS SEX DRINK EDU COUNT PERCENT 1 female coffee hs 100 10.0 2 female coffee coll 121 12.1 3 female coffee grad 114 11.4 4 female tea hs 55 5.5 5 female tea coll 47 4.7 6 female tea grad 43 4.3 7 male coffee hs 125 12.5 8 male coffee coll 122 12.2 9 male coffee grad 127 12.7 10 male tea hs 50 5.0 11 male tea coll 58 5.8 12 male tea grad 38 3.8 title 'proc tabulate sex * edu, drink'; proc tabulate data=sim; class sex drink edu; table sex * edu, drink; run; proc tabulate sex * edu, drink ---------------------------------------------- | | DRINK | | |-------------------------| | | coffee | tea | | |------------+------------| | | N | N | |------------------+------------+------------| |SEX |EDU | | | |--------+---------| | | |female |hs | 100.00| 55.00| | |---------+------------+------------| | |coll | 121.00| 47.00| | |---------+------------+------------| | |grad | 114.00| 43.00| |--------+---------+------------+------------| |male |hs | 125.00| 50.00| | |---------+------------+------------| | |coll | 122.00| 58.00| | |---------+------------+------------| | |grad | 127.00| 38.00| ---------------------------------------------- title 'proc tabulate with no formchars'; proc tabulate data=sim formchar=" "; class sex drink edu; table sex * edu, drink; run; proc tabulate with no formchars DRINK coffee tea N N SEX EDU female hs 100.00 55.00 coll 121.00 47.00 grad 114.00 43.00 male hs 125.00 50.00 coll 122.00 58.00 grad 127.00 38.00 title 'proc tabulate sex * edu, drink (labels suppressed)'; proc tabulate data=sim; class sex drink edu; table sex=" " * edu=" ", drink=" "/box = 'sex & education'; keylabel n=" "; run; proc tabulate sex * edu, drink (labels suppressed) ---------------------------------------------- |sex & education | coffee | tea | |------------------+------------+------------| |female |hs | 100.00| 55.00| | |---------+------------+------------| | |coll | 121.00| 47.00| | |---------+------------+------------| | |grad | 114.00| 43.00| |--------+---------+------------+------------| |male |hs | 125.00| 50.00| | |---------+------------+------------| | |coll | 122.00| 58.00| | |---------+------------+------------| | |grad | 127.00| 38.00| ---------------------------------------------- title 'proc means with class statement'; proc means data=sim n mean; class sex drink edu; var income; run; proc means with class statement Analysis Variable : INCOME SEX DRINK EDU N Obs N Mean ----------------------------------------------- female coffee hs 100 100 21003.69 coll 121 121 20186.04 grad 114 114 20327.59 tea hs 55 55 19910.96 coll 47 47 19762.58 grad 43 43 18691.87 male coffee hs 125 125 20480.80 coll 122 122 20021.40 grad 127 127 19849.04 tea hs 50 50 19108.01 coll 58 58 20895.58 grad 38 38 18546.37 ----------------------------------------------- title 'proc summary (output data set)'; proc summary data=sim; class sex drink edu; var income; output out=new n=count mean=average; run; proc print split="#" data=new; label average = "Average#Income"; run; proc summary (output data set) Average OBS SEX DRINK EDU _TYPE_ _FREQ_ COUNT Income 1 . . . 0 1000 1000 20083.56 2 . . hs 1 330 330 20336.28 3 . . coll 1 348 348 20189.39 4 . . grad 1 322 322 19710.20 5 . coffee . 2 709 709 20287.40 6 . tea . 2 291 291 19586.95 7 . coffee hs 3 225 225 20713.19 8 . coffee coll 3 243 243 20103.38 9 . coffee grad 3 241 241 20075.41 10 . tea hs 3 105 105 19528.61 11 . tea coll 3 105 105 20388.43 12 . tea grad 3 81 81 18623.61 13 female . . 4 480 480 20183.17 14 male . . 4 520 520 19991.62 15 female . hs 5 155 155 20615.95 16 female . coll 5 168 168 20067.57 17 female . grad 5 157 157 19879.59 18 male . hs 5 175 175 20088.57 19 male . coll 5 180 180 20303.08 20 male . grad 5 165 165 19549.03 21 female coffee . 6 335 335 20478.28 22 female tea . 6 145 145 19501.34 23 male coffee . 6 374 374 20116.41 24 male tea . 6 146 146 19671.96 25 female coffee hs 7 100 100 21003.69 26 female coffee coll 7 121 121 20186.04 27 female coffee grad 7 114 114 20327.59 28 female tea hs 7 55 55 19910.96 29 female tea coll 7 47 47 19762.58 30 female tea grad 7 43 43 18691.87 31 male coffee hs 7 125 125 20480.80 32 male coffee coll 7 122 122 20021.40 33 male coffee grad 7 127 127 19849.04 34 male tea hs 7 50 50 19108.01 35 male tea coll 7 58 58 20895.58 36 male tea grad 7 38 38 18546.37 title 'proc means with by statement (needs sort)'; proc sort data=sim; by sex drink edu; run; proc means data=sim n mean; by sex drink edu; var income; run; proc means with by statement (needs sort) Analysis Variable : INCOME ------------------------- SEX=female DRINK=coffee EDU=hs ----------------------- N Mean ------------------ 100 21003.69 ------------------ ------------------------ SEX=female DRINK=coffee EDU=coll ---------------------- N Mean ------------------ 121 20186.04 ------------------ ------------------------ SEX=female DRINK=coffee EDU=grad ---------------------- N Mean ------------------ 114 20327.59 ------------------ -------------------------- SEX=female DRINK=tea EDU=hs ------------------------- N Mean ------------------ 55 19910.96 ------------------ . . . title 'proc tabulate with analysis variable'; proc tabulate data=sim; class sex drink edu; var income; table sex * edu * income * mean, drink; run; proc tabulate with analysis variable ---------------------------------------------- | | DRINK | | |-------------------------| | | coffee | tea | |------------------+------------+------------| |SEX|EDU| | | | | |---+---+---+------| | | |fe-|hs |IN-|MEAN | | | |ma-| |CO-| | | | |le | |ME | | 21003.69| 19910.96| | |---+---+------+------------+------------| | |co-|IN-|MEAN | | | | |ll |CO-| | | | | | |ME | | 20186.04| 19762.58| | |---+---+------+------------+------------| | |gr-|IN-|MEAN | | | | |ad |CO-| | | | | | |ME | | 20327.59| 18691.87| |---+---+---+------+------------+------------| |ma-|hs |IN-|MEAN | | | |le | |CO-| | | | | | |ME | | 20480.80| 19108.01| | |---+---+------+------------+------------| | |co-|IN-|MEAN | | | | |ll |CO-| | | | | | |ME | | 20021.40| 20895.58| | |---+---+------+------------+------------| | |gr-|IN-|MEAN | | | | |ad |CO-| | | | | | |ME | | 19849.04| 18546.37| ---------------------------------------------- proc tabulate with analysis variable (rts=50) title 'proc tabulate with analysis variable (rts=50)'; proc tabulate data=sim; class sex drink edu; var income; table sex * edu * income * mean, drink/rts=50; run; ---------------------------------------------------------------------------- | | DRINK | | |-------------------------| | | coffee | tea | |------------------------------------------------+------------+------------| |SEX |EDU | | | | | |-----------+-----------+-----------+------------| | | |female |hs |INCOME |MEAN | 21003.69| 19910.96| | |-----------+-----------+------------+------------+------------| | |coll |INCOME |MEAN | 20186.04| 19762.58| | |-----------+-----------+------------+------------+------------| | |grad |INCOME |MEAN | 20327.59| 18691.87| |-----------+-----------+-----------+------------+------------+------------| |male |hs |INCOME |MEAN | 20480.80| 19108.01| | |-----------+-----------+------------+------------+------------| | |coll |INCOME |MEAN | 20021.40| 20895.58| | |-----------+-----------+------------+------------+------------| | |grad |INCOME |MEAN | 19849.04| 18546.37| ---------------------------------------------------------------------------- title 'proc tabulate with analysis variable, labels supressed, row=float'; proc tabulate data=sim; class sex drink edu; var income; table sex=" " * edu=" "*income=" "*mean, drink=" "/box = 'sex & education' row=float; keylabel n=" " mean=" "; run; proc tabulate with analysis variable, labels supressed, row=float ---------------------------------------------- |sex & education | coffee | tea | |------------------+------------+------------| |female |hs | 21003.69| 19910.96| | |---------+------------+------------| | |coll | 20186.04| 19762.58| | |---------+------------+------------| | |grad | 20327.59| 18691.87| |--------+---------+------------+------------| |male |hs | 20480.80| 19108.01| | |---------+------------+------------| | |coll | 20021.40| 20895.58| | |---------+------------+------------| | |grad | 19849.04| 18546.37| ---------------------------------------------- title 'proc tabulate with analysis variable, keylabels, row=float'; proc tabulate data=sim; class sex drink edu; var income; table sex=" " * edu=" ", drink*(income=" "*n*f=5. income=" "*mean) /box = 'sex & education' row=float; keylabel n="Count" mean="Average"; run; proc tabulate with analysis variable, keylabels, row=float ---------------------------------------------------------- |sex & education | DRINK | | |-------------------------------------| | | coffee | tea | | |------------------+------------------| | |Count| Average |Count| Average | |------------------+-----+------------+-----+------------| |female |hs | 100| 21003.69| 55| 19910.96| | |---------+-----+------------+-----+------------| | |coll | 121| 20186.04| 47| 19762.58| | |---------+-----+------------+-----+------------| | |grad | 114| 20327.59| 43| 18691.87| |--------+---------+-----+------------+-----+------------| |male |hs | 125| 20480.80| 50| 19108.01| | |---------+-----+------------+-----+------------| | |coll | 122| 20021.40| 58| 20895.58| | |---------+-----+------------+-----+------------| | |grad | 127| 19849.04| 38| 18546.37| ---------------------------------------------------------- title 'proc tabulate using all for overall summaries'; options ls=100; proc tabulate data=sim; class sex drink edu; var income; table sex=" " * edu=" ", drink*(income=" "*n*f=5. income=" "*mean) all*income=" "*n*f=5. all*income=" "*mean /box = 'sex & education' row=float; keylabel n="Count" mean="Average"; run; proc tabulate using all for overall summaries ---------------------------------------------------------------------------------- |sex & education | DRINK | | | | |-------------------------------------| | | | | coffee | tea | ALL | ALL | | |------------------+------------------+-----+------------| | |Count| Average |Count| Average |Count| Average | |-----------------------+-----+------------+-----+------------+-----+------------| |female |hs | 100| 21003.69| 55| 19910.96| 155| 20615.95| | |-----------+-----+------------+-----+------------+-----+------------| | |coll | 121| 20186.04| 47| 19762.58| 168| 20067.57| | |-----------+-----+------------+-----+------------+-----+------------| | |grad | 114| 20327.59| 43| 18691.87| 157| 19879.59| |-----------+-----------+-----+------------+-----+------------+-----+------------| |male |hs | 125| 20480.80| 50| 19108.01| 175| 20088.57| | |-----------+-----+------------+-----+------------+-----+------------| | |coll | 122| 20021.40| 58| 20895.58| 180| 20303.08| | |-----------+-----+------------+-----+------------+-----+------------| | |grad | 127| 19849.04| 38| 18546.37| 165| 19549.03| ---------------------------------------------------------------------------------- title 'proc tabulate using grouped overall summaries'; proc tabulate data=sim; class sex drink edu; var income; table sex=" " * edu=" ", drink*(income=" "*n*f=5. income=" "*mean) all="Overall"*(income=" "*n*f=5. income=" "*mean) /box = 'sex & education' row=float; keylabel n="Count" mean="Average"; run; proc tabulate using grouped overall summaries ---------------------------------------------------------------------------------- |sex & education | DRINK | | | |-------------------------------------| | | | coffee | tea | Overall | | |------------------+------------------+------------------| | |Count| Average |Count| Average |Count| Average | |-----------------------+-----+------------+-----+------------+-----+------------| |female |hs | 100| 21003.69| 55| 19910.96| 155| 20615.95| | |-----------+-----+------------+-----+------------+-----+------------| | |coll | 121| 20186.04| 47| 19762.58| 168| 20067.57| | |-----------+-----+------------+-----+------------+-----+------------| | |grad | 114| 20327.59| 43| 18691.87| 157| 19879.59| |-----------+-----------+-----+------------+-----+------------+-----+------------| |male |hs | 125| 20480.80| 50| 19108.01| 175| 20088.57| | |-----------+-----+------------+-----+------------+-----+------------| | |coll | 122| 20021.40| 58| 20895.58| 180| 20303.08| | |-----------+-----+------------+-----+------------+-----+------------| | |grad | 127| 19849.04| 38| 18546.37| 165| 19549.03| ---------------------------------------------------------------------------------- title 'proc tabulate using grouped overall summaries on rows'; proc tabulate data=sim; class sex drink edu; var income; table (sex=" " * edu=" ") * (income=" "*n*f=5. income=" "*mean) all="Overall"*(income=" "*n*f=5. income=" "*mean), drink /box = 'sex & education' row=float; keylabel n="Count" mean="Average"; run; proc tabulate using grouped overall summaries on rows --------------------------------------------------- |sex & education | DRINK | | |-------------------------| | | coffee | tea | |-----------------------+------------+------------| |female |hs |Count | 100| 55| | | |-------+------------+------------| | | |Average| 21003.69| 19910.96| | |-------+-------+------------+------------| | |coll |Count | 121| 47| | | |-------+------------+------------| | | |Average| 20186.04| 19762.58| | |-------+-------+------------+------------| | |grad |Count | 114| 43| | | |-------+------------+------------| | | |Average| 20327.59| 18691.87| |-------+-------+-------+------------+------------| |male |hs |Count | 125| 50| | | |-------+------------+------------| | | |Average| 20480.80| 19108.01| | |-------+-------+------------+------------| | |coll |Count | 122| 58| | | |-------+------------+------------| | | |Average| 20021.40| 20895.58| | |-------+-------+------------+------------| | |grad |Count | 127| 38| | | |-------+------------+------------| | | |Average| 19849.04| 18546.37| |-----------------------+------------+------------| |Overall |Count | 709| 291| | |-----------+------------+------------| | |Average | 20287.40| 19586.95| ---------------------------------------------------