proc tabulate by example code for data

Download Proc Tabulate by Example Code for Data

If you can't read please download the document

Upload: myscribe55

Post on 05-Sep-2014

62 views

Category:

Documents


4 download

TRANSCRIPT

/*--------------------------------------------------------------------*/ /* PROC TABULATE by Example */ /* by Lauren Haworth */ /* Copyright(c) 1999 by SAS Institute Inc., Cary, NC, USA */ /* SAS Publications order # 56514 */ /* ISBN 1-58025-358-X*/ /*-------------------------------------------------------------------*/ /* */ /* This material is provided "as is" by SAS Institute Inc. There */ /* are no warranties, expressed or implied, as to merchantability or */ /* fitness for a particular purpose regarding the materials or code */ /* contained herein. The Institute is not responsible for errors */ /* in this material as it now exists or will exist, nor does the */ /* Institute provide technical support for it. */ /*-------------------------------------------------------------------*/ /* Date last updated: 1Jun00 */ /*-------------------------------------------------------------------*/ /* Questions or problem reports concerning this material may be */ /* addressed to the author: */ /* */ /* SAS Institute Inc. */ /* Books by Users */ /* Attn: Lauren Haworth */ /* SAS Campus Drive */ /* Cary, NC 27513 */ /* */ /* */ /* If you prefer, you can send email to: [email protected] */ /* Use this for subject field: */ /* Comments for Lauren Haworth */ /* */ /*-------------------------------------------------------------------*/ /**********************************************************/ /* CREATE DATASETS USED BY SAMPLE CODE */ /* To run the examples for a given chapter, */ /* (1) run the options, libname and proc format */ /* code below, and */ /* (2) create the dataset SAMPLES using the input */ /* statement below */ /* (3) create the datasets named for the chapter */ /* (CHPTxx) */ /* (4) run the code for the */ /* examples */ /**********************************************************/ options ls=132 nofmterr nocenter nodate nonumber FORMCHAR='|----|+|---'; libname sd2 "C:\BOOK\SAS"; PROC FORMAT; VALUE AGEFT VALUE EDUC VALUE EDFT LOW-4 THEN OCCUP=OCCUP-4; IF OCCUP IN(1,2,3,4); EMPYEARS=(AGE/4)-occup-sector-genDNUM; EMPYEARS=ABS(EMPYEARS); IF EMPYEARS=0 THEN EMPYEARS=EMPYEARS+4; RUN;DATA CHPT20A; FORMAT EDUC EDFT. MARITAL MARFT. FNAME $10. LNAME $15.; INPUT FNAME $ LNAME $ YEAR AGE EDUC INCOME MARITAL NUMKIDS; CARDS; JOHN JOHNSON 1998 40 15 0 0 0 JOHN JOHNSON 1999 41 156 32000 0 0 JANE DOE 1998 28 17 75000 1 0 JANE DOE 1999 29 17 77000 1 0 TIM SAUNDERS 1998 21 12 15000 1 0 TIM SAUNDERS 1999 22 12 16000 1 1 TINA SMITH 1998 20 12 5000 1 2 TINA SMITH 1999 21 12 4000 1 2 TOM LANDRY 1998 34 16 65400 2 0 TOM LANDRY 1999 35 16 69200 2 0 DONNA JONES 1998 38 16 54000 2 3 DONNA JONES 1999 39 16 50000 1 4 DOUG ANDERSON 1998 57 16 78000 1 5 DOUG ANDERSON 1999 58 16 80000 1 5 SAMANTHA LEE 1998 55 16 89000 1 1 SAMANTHA LEE 1999 56 16 0 1 1 ; RUN; PROC SORT DATA=CHPT20A; BY FNAME LNAME; RUN; DATA CHPT20B; SET SAMPLES; GENDER=GENDNUM; DROP GENDCHAR GENDNUM; FORMAT GENDER GENDER. AGE AGEFT.; IF OCCUP>4 THEN OCCUP=OCCUP-3; IF OCCUP>4 THEN OCCUP=OCCUP-4; IF OCCUP IN(1,2,3,4); IF AGE4 THEN OCCUP=OCCUP-3; IF OCCUP>4 THEN OCCUP=OCCUP-4; IF OCCUP IN(1,2,3,4); IF GENDER=2 THEN SCORE1=RANNOR(122870)*10; ELSE SCORE1=RANNOR(062169)*30; IF AGE30 THEN SCORE3=INCOME/2300; ELSE SCORE3=RANNOR(11166)*50; SCORE1=SCORE1+10; SCORE2=SCORE2+10; SCORE3=SCORE3+10; AGEGRP=PUT(AGE, AGEFT.); RUN; PROC SORT DATA=CHPT20C; BY AGEGRP GENDER; RUN;DATA CHPT23; SET SAMPLES; GENDER=GENDNUM; DROP GENDCHAR GENDNUM; FORMAT GENDER GENDER.; RUN; DATA CHPT24; SET SAMPLES; GENDER=GENDNUM; IF GENDER=2 THEN GENDER=1; ELSE IF GENDER=1 THEN GENDER=2; DROP GENDCHAR GENDNUM; FORMAT GENDER GENDER.; IF OCCUP>4 THEN OCCUP=OCCUP-3; IF OCCUP>4 THEN OCCUP=OCCUP-4; IF OCCUP IN(1,2,3,4); if race=1 and mod(_N_,34)=0 then race=5; else if race=1 and mod(_N_,26)=0 then race=4; else if race=1 and mod(_N_,22)=0 then race=3; else if race=1 and mod(_N_,14)=0 then race=2; format race NEWRACE.; YEAR=YEAR+2; IF AGE>45 THEN UNION=.; IF AGE5 then JobClass=' Clerical'; else JobClass='Manufacturing'; RUN; DATA CHPT24F; SET CHPT24E (RENAME=(GENDER=GENDTEMP)); IF GENDTEMP='Female' THEN GENDER=1; IF GENDTEMP='Male' THEN GENDER=2;FORMAT GENDER GENDREV.; if union then ans1=1; else ans1=0; if gender=2 then ans2=1; else ans2=0; if numkids>0 then ans3=1; else ans3=0; if occup>3 then ans4=1; else ans4=0; if sector=1 then ans5=1; else ans5=0; label ans1='Prices' ans2='Service' ans3='Location' ans4='Hours' ans5='Variety'; FORMAT ANS1-ANS5 YESNOFT.; RUN; DATA TEMPANS; SET CHPT24F; ARRAY ANS{5} ANS1-ANS5; IF SUM(ANS1,ANS2,ANS3,ANS4,ANS5)>0; DO A=1 TO 5; IF ANS{A} NE . THEN DO; RESP=A; ANSWER=ANS{A}; OUTPUT; END; END; RUN; PROC SUMMARY DATA=TEMPANS NWAY; CLASS RESP; VAR ANSWER; OUTPUT OUT=CHPT24G MEAN=; RUN; PROC SORT DATA=CHPT24G; BY DESCENDING ANSWER; RUN; /**********************************************************/ /* Chapter 1: Why Use PROC TABULATE? */ /**********************************************************/ DATA TEMP; SET CHPT01; RUN; /* Example Without Using PROC TABULATE*/ /* Output 1.1 */ PROC MEANS; VAR AGE INCOME EDUC; RUN; /* Output 1.2 */ PROC MEANS; BY GENDER; VAR AGE INCOME EDUC; RUN; /* Example With PROC TABULATE */ /* Output 1.3 */ PROC TABULATE; CLASS GENDER; VAR AGE INCOME EDUC;TABLE (AGE INCOME EDUC)*MEAN, GENDER ALL; RUN; /**********************************************************/ /* Chapter 3: Before You Start Writing PROC TABULATE Code */ /**********************************************************/ DATA TESTDAT; SET CHPT03; RUN; /* Examining the Data*/ /* Output 3.1 */ PROC CONTENTS DATA=TESTDAT; RUN; /* Output 3.2 */ PROC PRINT DATA=TESTDAT (OBS=50); VAR ID EDUC INCOME AGE; FORMAT _ALL_; TITLE 'SAMPLE RECORDS FOR REVIEW'; RUN; /* Output 3.3 */ PROC FREQ DATA=TESTDAT; TABLES EDUC INCOME AGE; TITLE 'FREQUENCIES FOR REVIEW'; RUN; /* Output 3.4 */ PROC FORMAT; VALUE ED2FT0-15='NO COLLEGE DEGREE' 16='COLLEGE DEGREE';RUN; TITLE; PROC FREQ DATA=TESTDAT; WHERE EDUC5 THEN SAT=SAT-5; RUN; /* Output 8.9 */ PROC TABULATE DATA=TEMP; CLASS GENDER RACE SAT; VAR INCOME; TABLE GENDER RACE, SAT*PCTN INCOME*MEAN; RUN; /* Output 8.10 */ PROC TABULATE DATA=TEMP; CLASS GENDER RACE SAT; VAR INCOME; TABLE GENDER, SAT*PCTSUM*INCOME MEAN*INCOME; RUN; /* Output 8.11 */ PROC TABULATE DATA=TEMP F=10.2; CLASS GENDER RACE SAT; TABLE GENDER*PCTN RACE*N, SAT / rts=20; RUN; /* Percentages With Subtotals*/ /* Output 8.12 */ PROC TABULATE DATA=TEMP; CLASS GENDER RACE CUSTTYPE; TABLE RACE*GENDER ALL, CUSTTYPE*PCTN; RUN;/* Output 8.14 */ PROC TABULATE DATA=TEMP; CLASS GENDER RACE CUSTTYPE; TABLE RACE*(GENDER ALL) ALL, CUSTTYPE*PCTN; RUN; /* Percentage Subtotals on Both Dimensions*/ /* Output 8.15 */ PROC TABULATE DATA=TEMP; CLASS GENDER RACE CUSTTYPE SIZE; TABLE RACE*(GENDER ALL) ALL, (CUSTTYPE*(SIZE ALL) ALL)*N; RUN; /* Output 8.18 */ PROC TABULATE DATA=TEMP; CLASS GENDER RACE CUSTTYPE SIZE; TABLE RACE*(GENDER ALL) ALL, (CUSTTYPE*(SIZE ALL) ALL)* PCTN; RUN; /* Percentages for Three-Dimensional Tables*/ PROC FORMAT; VALUE SATFT 1,2,3='POOR-GOOD' 4,5='VERY GOOD-EXCELLENT';RUN; DATA TEMP; SET CHPT08; FORMAT SAT SATFT.; IF SAT>5 THEN SAT=SAT-5; RUN; /* Output 8.17 */ PROC TABULATE DATA=TEMP; CLASS SAT RACE GENDER; TABLE SAT ALL, RACE ALL, (GENDER ALL)* PCTN; RUN; /* Percentages for Analysis Variables*/ DATA TEMP; SET CHPT08; NEWINC=INCOME*2*ABS(RANUNI(11166)); LABEL NEWINC='New Income' INCOME='Old Income'; RUN; /* Output 8.18 */ PROC TABULATE DATA=TEMP; CLASS GENDER EDUC; VAR INCOME NEWINC; TABLE GENDER*EDUC, INCOME*MEAN NEWINC*MEAN; RUN; /* Output 8.19 */ PROC TABULATE DATA=TEMP;CLASS GENDER EDUC; VAR INCOME NEWINC; TABLE GENDER*EDUC, INCOME*MEAN NEWINC*MEAN NEWINC*PCTSUM; RUN; /**********************************************************/ /* Chapter 9: Handling Missing Data */ /**********************************************************/ /* Missing data: how to find it*/ DATA TEMP; SET CHPT09; RUN; PROC FORMAT; VALUE UNION 0='Non-Union' 1='Union'; RUN; /* Output 9.1 */ PROC TABULATE DATA=TEMP; CLASS OCCUP UNION; VAR NUMEMP; TABLE OCCUP*UNION ALL, NUMEMP; RUN; /* Output 9.2 */ PROC TABULATE DATA=TEMP MISSING; CLASS OCCUP UNION; VAR NUMEMP; TABLE OCCUP*UNION ALL, NUMEMP; RUN; /* Reporting the missing data*/ /* Output 9.3 */ PROC TABULATE DATA=TEMP; CLASS OCCUP UNION; VAR NUMEMP; TABLE OCCUP*NUMEMP, UNION*N; RUN; /* Output 9.4 */ PROC TABULATE DATA=TEMP; CLASS OCCUP UNION; VAR NUMEMP; TABLE OCCUP*NUMEMP, UNION*NMISS; RUN; /* Output 9.5 */ PROC TABULATE DATA=TEMP; CLASS OCCUP UNION; VAR NUMEMP; TABLE OCCUP*NUMEMP, UNION*(N NMISS); RUN; /* Recoding the data*/ DATA TEMP;SET CHPT09; UNION=UNION+1; RUN; /* Output 9.6 */ DATA FIXED; SET TEMP; IF UNION=. THEN UNION=0; RUN; PROC FORMAT; VALUE MISSFT 0='Unknown' 1='Non-Union' 2='Union'; RUN; PROC TABULATE DATA=FIXED; CLASS OCCUP UNION; VAR NUMEMP; FORMAT UNION MISSFT.; TABLE OCCUP*UNION, NUMEMP; RUN; DATA TEMP; SET TEMP; OUTPUT; IF _N_=1 THEN DO; OCCUP=.; NUMEMP=2500; OUTPUT; END; RUN; /* Output 9.7 */ PROC FORMAT; VALUE MISSFT .='Unknown' 1='Non-Union' 2='Union'; RUN; PROC TABULATE DATA=TEMP MISSING; CLASS OCCUP UNION; VAR NUMEMP; FORMAT UNION MISSFT.; TABLE OCCUP*UNION, NUMEMP; RUN; /* Formatting missing values*/ DATA TEMP; SET CHPT09; RUN; /* Output 9.8 */ PROC TABULATE DATA=TEMP; CLASS OCCUP UNION; VAR NUMEMP; TABLE OCCUP*UNION ALL, NUMEMP; RUN; /* Output 9.9 */ PROC TABULATE DATA=TEMP; CLASS OCCUP UNION; VAR NUMEMP; TABLE OCCUP*UNION ALL, NUMEMP / MISSTEXT="no data";RUN; /* Three-dimensional tables with missing data*/ /* Output 9.10 */ PROC TABULATE DATA=TEMP; CLASS UNION OCCUP GENDER; VAR NUMEMP; TABLE GENDER, UNION, OCCUP*NUMEMP*SUM; RUN; /* Output 9.11 */ PROC TABULATE DATA=TEMP; CLASS UNION OCCUP GENDER; VAR NUMEMP; TABLE GENDER, UNION, OCCUP*NUMEMP*SUM / PRINTMISS; RUN; /* Breaking up the table*/ DATA TEMP; SET CHPT09A; RUN; /* Output 9.12 */ PROC TABULATE DATA=TEMP MISSING; CLASS MARITAL NUMKIDS EDUC; TABLE EDUC ALL, (MARITAL NUMKIDS)*N; RUN; /* Output 9.13 */ PROC TABULATE DATA=TEMP; CLASS MARITAL EDUC; TABLE EDUC ALL, MARITAL*N; RUN; PROC TABULATE DATA=TEMP; CLASS NUMKIDS EDUC; TABLE EDUC ALL, NUMKIDS*N; RUN; /* Data step tricks*/ DATA TEMP; SET CHPT09B; RUN; /* Output 9.14 */ PROC TABULATE DATA=TEMP; CLASS OCCUP SECTOR; VAR NUMEMP; TABLE OCCUP ALL, SECTOR*NUMEMP; RUN; DATA TEMP; SET CHPT09B (RENAME=(SECTOR=SECTNUM)); IF SECTNUM NE .; FORMAT SECTOR $10.; SECTOR=PUT(SECTNUM,SECTOR.); RUN;/* Output 9.1 */ DATA FIXIT; SET TEMP; OUTPUT; IF _N_=1 THEN DO N=1 TO 4; OCCUP=N; SECTOR="Non-Profit"; NUMEMP=0; OUTPUT; END; RUN; PROC TABULATE DATA=FIXIT; CLASS OCCUP SECTOR; VAR NUMEMP; TABLE OCCUP ALL, SECTOR*NUMEMP; RUN; /**********************************************************/ /* Chapter 10: Modifying Row and Column Headings */ /**********************************************************/ DATA TEMP; SET CHPT10; RUN; /* Modifying variable labels*/ /* Output 10.1 */ PROC TABULATE DATA=TEMP; CLASS OCCUP FULLTIME; VAR INCOME; TABLE OCCUP ALL, (FULLTIME ALL)*INCOME*MEAN; LABEL OCCUP='Occupation' FULLTIME='Employment Status'; KEYLABEL ALL='TOTAL'; RUN; /* Output 10.2 */ PROC TABULATE DATA=TEMP; CLASS OCCUP FULLTIME; VAR INCOME; TABLE OCCUP="Occupation" ALL="All Occupations", (FULLTIME="Employment Status" ALL="Total")*INCOME*MEAN; RUN; /* Modifying statistic labels*/ /* Output 10.3 */ PROC TABULATE DATA=TEMP; CLASS OCCUP FULLTIME; VAR INCOME; TABLE OCCUP, FULLTIME*INCOME*(N MEAN STD); RUN; /* Output 10.4 */ PROC TABULATE DATA=TEMP; CLASS OCCUP FULLTIME;VAR INCOME; TABLE OCCUP, FULLTIME*INCOME* (N="NUMBER OF OBS." MEAN STD="STANDARD DEVIATION"); RUN; /* Another way to modify statistic labels*/ /* Output 10.5 */ PROC TABULATE DATA=TEMP; CLASS OCCUP FULLTIME; VAR INCOME; TABLE OCCUP, FULLTIME*INCOME*(N MEAN STD); KEYLABEL N="NUMBER OF OBS." STD="STANDARD DEVIATION"; RUN; DATA TEMP; SET CHPT10A; RUN; /* Output 10.6 */ PROC TABULATE DATA=TEMP; CLASS EDUC RACE FULLTIME; VAR AGE; TABLE EDUC*(N MEAN) FULLTIME*(NMISS MEAN), RACE*AGE; KEYLABEL MEAN="Average"; RUN; /* Hiding statistic labels*/ DATA TEMP; SET CHPT10B; RUN; /* Output 10.7 */ PROC TABULATE DATA=TEMP; CLASS YEAR FULLTIME GENDER; VAR INCOME; TABLE YEAR, FULLTIME*GENDER*INCOME*MEAN; RUN; /* Output 10.8 */ PROC TABULATE DATA=TEMP; CLASS YEAR FULLTIME GENDER; VAR INCOME; TABLE YEAR, FULLTIME*GENDER*INCOME*MEAN=" " / BOX="MEAN"; RUN; /* Hiding variable labels*/ /* Output 10.9 */ PROC TABULATE DATA=TEMP; CLASS YEAR FULLTIME GENDER; VAR INCOME; TABLE YEAR, FULLTIME*GENDER=" "*INCOME*MEAN=" " / BOX="Mean"; RUN; DATA TEMP;SET CHPT10B; IF FULLTIME=2 THEN FULLTIME=1; ELSE IF FULLTIME IN(3,4,5) THEN FULLTIME=2; RUN; /* Output 10.10 */ PROC FORMAT; VALUE FULLTIME 1="Employed Full-Time" 2="Employed Part-Time"; RUN: PROC TABULATE DATA=TEMP; CLASS YEAR FULLTIME GENDER; VAR INCOME; TABLE YEAR=" ", FULLTIME=" "*GENDER=" "*INCOME=" "*MEAN=" " / BOX="Mean Income"; FORMAT FULLTIME FULLTIME.; RUN; /* Modifying row headings*/ DATA TEMP; SET CHPT10C; RUN; PROC FORMAT; VALUE FULLTIME 2='Full-time' 3,4,5='Part-time'; RUN; /* Output 10.11 */ PROC TABULATE DATA=TEMP; CLASS EDUC GENDER FULLTIME; VAR INCOME; TABLE EDUC="Education"*GENDER=" "*INCOME=" "*MEAN=" ", FULLTIME=" " / BOX="Mean Income"; RUN; /* Output 10.12 */ PROC TABULATE DATA=TEMP; CLASS EDUC GENDER FULLTIME; VAR INCOME; TABLE EDUC="Education"*GENDER=" "*INCOME=" "*MEAN=" ", FULLTIME=" " / BOX="Mean Income" ROW=FLOAT; RUN; /* Modifying row heading widths*/ DATA TEMP; SET CHPT10; RUN; /* Output 10.13 */ OPTIONS LINESIZE=132; PROC TABULATE DATA=TEMP; CLASS EDUC GENDER OCCUP; VAR INCOME; TABLE EDUC="Education"*GENDER=" ", OCCUP="Occupation"*INCOME=" "*MEAN=" " / BOX="Mean Income"; RUN;/* Output 10.14 */ PROC TABULATE DATA=TEMP; CLASS EDUC GENDER OCCUP; VAR INCOME; TABLE EDUC="Education"*GENDER=" ", OCCUP="Occupation"*INCOME=" "*MEAN=" " / BOX="Mean Income" RTS=24; RUN; /* Modifying row heading indents*/ DATA TEMP; SET CHPT10D; RUN; /* Output 10.15 */ PROC TABULATE DATA=TEMP; CLASS EDUC GENDER RACE OCCUP; VAR INCOME; TABLE EDUC="Education"*GENDER=" "*RACE=" ", OCCUP="Occupation"*INCOME=" "*MEAN=" " / BOX="Mean Income" RTS=32; RUN; /* Output 10.16 */ PROC TABULATE DATA=TEMP; CLASS EDUC GENDER RACE OCCUP; VAR INCOME; TABLE EDUC="Education"*GENDER=" "*RACE=" ", OCCUP="Occupation"*INCOME=" "*MEAN=" " / BOX="Mean Income" RTS=20 INDENT=5; RUN; /* Modifying column widths*/ DATA TEMP; SET CHPT10C; IF RACE IN(1,2); IF EDUC5 THEN SAT=SAT-5; FORMAT SAT SATFT.; GENDER=GENDNUM; FORMAT GENDER GENDER.; RUN; PROC SORT DATA=TEMP; BY DESCENDING SAT; RUN; /* Output 10.20 */ PROC TABULATE DATA=TEMP ORDER=INTERNAL; CLASS GENDER SAT; TABLE GENDER=' ', SAT*N / RTS=15; RUN; /* Output 10.21 */ PROC TABULATE DATA=TEMP ORDER=DATA; CLASS GENDER SAT; TABLE GENDER=' ', SAT*N / RTS=15; RUN; /* Output 10.22 */ PROC TABULATE DATA=TEMP ORDER=FORMATTED; CLASS GENDER SAT; TABLE GENDER=' ', SAT*N / RTS=15; RUN; /* Output 10.23 */ PROC TABULATE DATA=TEMP ORDER=FREQ; CLASS GENDER SAT; TABLE GENDER=' ' ALL, SAT*N / RTS=15; RUN; /* Reordering the headings*/ PROC FORMAT; VALUE COURSEFT 101="Writing" 104="Arithmetic" 109="Reading"; RUN;DATA TEMP; SET CHPT10E; RUN; /* Output 10.24 */ PROC TABULATE DATA=TEMP; CLASS COURSE GENDER; FORMAT COURSE COURSEFT.; VAR SCORE; TABLE COURSE*MEAN=" ", (GENDER=" " ALL)*SCORE="Mean Score" / ROW=FLOAT; RUN; /* Output 10.25 */ PROC TABULATE DATA=TEMP ORDER=FORMATTED; CLASS COURSE GENDER; FORMAT COURSE COURSEFT.; VAR SCORE; TABLE COURSE*MEAN=" ", (GENDER=" " ALL)*SCORE="Mean Score" / ROW=FLOAT; RUN; /* Output 10.26 */ PROC FORMAT; VALUE COURSEFT 109=" Reading" 101=" Writing" 104="Arithmetic"; RUN; PROC TABULATE DATA=TEMP ORDER=FORMATTED; CLASS COURSE GENDER; FORMAT COURSE COURSEFT.; VAR SCORE; TABLE COURSE*MEAN=" ", (GENDER=" " ALL)*SCORE="Mean Score" / ROW=FLOAT; RUN; /**********************************************************/ /* Chapter 11: Formatting Table Values */ /**********************************************************/ /* Using appropriate formats - I*/ DATA TEMP; SET CHPT11; IF OCCUP IN(1,2,3,4); RUN; /* Output 11.1 */ PROC TABULATE DATA=TEMP; CLASS SECTOR OCCUP; VAR INCOME; TABLE OCCUP=' ', SECTOR=' '*INCOME*(N MEAN); RUN; /* Output 11.2 */ PROC TABULATE DATA=TEMP FORMAT=DOLLAR10.; CLASS SECTOR OCCUP; VAR INCOME;TABLE OCCUP=' ', SECTOR=' '*INCOME*(N MEAN); RUN; /* Output 11.3 */ PROC TABULATE DATA=TEMP; CLASS SECTOR OCCUP; VAR INCOME; TABLE OCCUP=' ', SECTOR=' '*INCOME*(N*F=6. MEAN*F=DOLLAR10.); RUN; /* Using Appropriate Formats - II*/ DATA TEMP; SET CHPT11; RUN; /* Output 11.4 */ PROC TABULATE DATA=TEMP; CLASS EDUC; VAR AGE INCOME; TABLE AGE*F=6.1 INCOME*F=DOLLAR10., EDUC*MEAN; RUN; /* Output 11.5 */ PROC TABULATE DATA=TEMP; CLASS EDUC; VAR AGE INCOME; TABLE AGE*F=6.1 INCOME*F=DOLLAR10., EDUC*(N MEAN); RUN; /* Output 11.6 */ PROC TABULATE DATA=TEMP; CLASS EDUC; VAR AGE INCOME; TABLE AGE INCOME, EDUC*(N*F=8. MEAN*F=8.1); RUN; /* Output 11.7 */ PROC TABULATE DATA=TEMP; CLASS EDUC; VAR AGE INCOME; TABLE AGE*F=6.1 INCOME*F=DOLLAR10., EDUC*(N*F=6. MEAN); RUN; /* Formatting percentages*/ DATA TEMP; SET CHPT11; IF OCCUP IN(1,2,3,4); RUN; /* Output 11.8 */ PROC TABULATE DATA=TEMP; CLASS OCCUP GENDER; TABLE OCCUP*PCTN=" ", GENDER ALL / ROW=FLOAT; RUN; /* Output 11.9 */PROC TABULATE DATA=TEMP; CLASS OCCUP GENDER; TABLE OCCUP*PCTN=" "*F=PERCENT9.1, GENDER ALL / ROW=FLOAT; RUN; /* Output 11.10 */ PROC FORMAT; PICTURE PCTPIC LOW-HIGH='000.0%'; RUN; PROC TABULATE DATA=TEMP; CLASS OCCUP GENDER; TABLE OCCUP*PCTN=" "*F=PCTPIC., GENDER ALL / ROW=FLOAT; RUN; /* Formatting very large numbers*/ DATA TEMPBIG; SET CHPT11; IF OCCUP IN(1,2,3,4); INCOME=INCOME*1041; RUN; /* Output 11.11 */ PROC TABULATE DATA=TEMPBIG; CLASS SECTOR OCCUP; VAR INCOME; TABLE SECTOR, OCCUP*INCOME=' '*SUM=' ' / BOX='Total Income' RTS=15; RUN; /* Output 11.12 */ PROC TABULATE DATA=TEMPBIG; CLASS SECTOR OCCUP; VAR INCOME; TABLE SECTOR, OCCUP*INCOME=' '*SUM=' '*F=DOLLAR15. / BOX='Total Income' RTS=15; RUN; /* Output 11.13 */ PROC FORMAT; PICTURE MILPIC LOW-HIGH='000,000' (PREFIX='$' MULT=.000001); RUN; PROC TABULATE DATA=TEMPBIG; CLASS SECTOR OCCUP; VAR INCOME; TABLE SECTOR, OCCUP*INCOME=' '*SUM=' '*F=MILPIC12. / BOX='Total Income (in Millions)' RTS=15; RUN; /* Weighting the results*/ DATA TEMP; SET CHPT11A; RUN; /* Output 11.14 */ PROC TABULATE DATA=TEMP F=6.; CLASS YEAR GENDER; VAR SAT;TABLE (YEAR=" " ALL), (GENDER=" " ALL)*SAT=" "*(N*F=6. MEAN*F=6.1) / RTS=20 BOX="Satisfaction Rating" ROW=FLOAT; RUN; /* Output 11.15 */ PROC TABULATE DATA=TEMP; CLASS YEAR GENDER; VAR SAT; WEIGHT THEWT; TABLE (YEAR=" " ALL), (GENDER=" " ALL)*SAT=" "*(SUMWGT="N"*F=6. MEAN*F=6.1) / RTS=20 BOX="Weighted Satisfaction Rating" ROW=FLOAT; RUN; /**********************************************************/ /* Chapter 12: Modifying the Table Grid */ /**********************************************************/ /* Left justifying the table*/ DATA TEMP; SET CHPT12; RUN; /* Output 12.1 */ OPTIONS CENTER; PROC TABULATE DATA=TEMP; CLASS OCCUP GENDER; VAR AGE; TABLE OCCUP=' ', GENDER=' '*AGE=' '*MEAN=' ' / BOX='Mean Age' RTS=15 ROW=FLOAT; TITLE 'THIS IS A CENTERED TITLE AND TABLE'; RUN; /* Output 12.2 */ OPTIONS NOCENTER; PROC TABULATE DATA=TEMP; CLASS OCCUP GENDER; VAR AGE; TABLE OCCUP=' ', GENDER=' '*AGE=' '*MEAN=' ' / BOX='Mean Age' RTS=15 ROW=FLOAT; TITLE 'THIS IS A LEFT-JUSTIFIED TITLE AND TABLE'; RUN; /* Removing the row dividers*/ /* Output 12.3 */ OPTIONS CENTER; TITLE; PROC TABULATE DATA=TEMP NOSEPS; CLASS EDUC RACE FULLTIME; VAR INCOME; TABLE EDUC=" "*(RACE=" " ALL), FULLTIME=" "*INCOME=" "*(N*F=6. MEAN*F=DOLLAR8.) / BOX="Income"; RUN; /* Output 12.4 */PROC TABULATE DATA=TEMP NOSEPS; CLASS EDUC RACE FULLTIME; VAR INCOME; TABLE EDUC=" "*(RACE=" " ALL="--ALL--"), FULLTIME=" "*INCOME=" "*(N*F=6. MEAN*F=DOLLAR8.) / BOX="Income" INDENT=3 RTS=12; RUN; /* Modifying the FORMCHAR setting*/ /* Output 12.5 */ PROC TABULATE DATA=TEMP FORMCHAR='|----|+|---'; CLASS GENDER EDUC; VAR AGE; TABLE EDUC=' ', GENDER=' '*AGE=' '*MEAN=' ' / BOX='Mean Age' RTS=14; RUN; /* Output 12.6 */ PROC TABULATE DATA=TEMP FORMCHAR='| ||| '; CLASS GENDER EDUC; VAR AGE; TABLE EDUC=' ', GENDER=' '*AGE=' '*MEAN=' ' / BOX='Mean Age' RTS=14; RUN; /* Output 12.7 */ PROC TABULATE DATA=TEMP FORMCHAR=' ----------'; CLASS GENDER EDUC; VAR AGE; TABLE EDUC=' ', GENDER=' '*AGE=' '*MEAN=' ' / BOX='Mean Age' RTS=14; RUN; /* Output 12.8 */ PROC TABULATE DATA=TEMP FORMCHAR=''; CLASS GENDER EDUC; VAR AGE; TABLE EDUC=' ', GENDER=' '*AGE=' '*MEAN=' ' / BOX='Mean Age' RTS=14; RUN; /* Fitting more tables on the page: CONDENSE and NOCONTINUED*/ /* Output 12.9 */ OPTIONS LS=90 NOCENTER; PROC TABULATE DATA=TEMP; CLASS GENDER EDUC RACE; TABLE RACE, GENDER=' '*EDUC=' '*N=' '/ BOX='Number of Observations'; RUN; /* Output 12.10 */ PROC TABULATE DATA=TEMP; CLASS GENDER EDUC RACE; TABLE RACE, GENDER=' '*EDUC=' '*N=' '/ BOX='Number of Observations' NOCONTINUED CONDENSE; RUN;/**********************************************************/ /* Chapter 13: Making the Table Fit On the Page */ /**********************************************************/ /* Reformatting class variables*/ OPTIONS LS=90 CENTER; DATA TEMP; SET CHPT13; RUN; /* Output 13.1 */ PROC TABULATE DATA=TEMP; CLASS RACE FULLTIME; VAR NUMKIDS; TABLE FULLTIME, RACE=' '*NUMKIDS=' '*(N MEAN) / BOX='Number of Children'; RUN; /* Output 13.2 */ PROC FORMAT; VALUE NEWRACE 1='White' 2='Black' 3-5='Other'; RUN; PROC TABULATE DATA=TEMP; CLASS RACE FULLTIME; FORMAT RACE NEWRACE.; VAR NUMKIDS; TABLE FULLTIME, RACE=' '*NUMKIDS=' '*(N MEAN) / BOX='Number of Children'; RUN; /* Reducing the space used by row headings*/ /* Output 13.3 */ PROC TABULATE DATA=TEMP; CLASS RACE FULLTIME; FORMAT RACE NEWRACE.; VAR NUMKIDS; TABLE FULLTIME, RACE=' '*NUMKIDS=' '*(N MEAN) / BOX='Number of Children' RTS=10; RUN; /* Output 13.4 */ PROC TABULATE DATA=TEMP; CLASS RACE FULLTIME; FORMAT RACE NEWRACE.; VAR NUMKIDS; TABLE FULLTIME, RACE=' '*NUMKIDS=' '*(N MEAN) / BOX='Number of Children' RTS=12; RUN; /* Reducing the column widths*/ /* Output 13.5 */ PROC TABULATE DATA=TEMP; CLASS RACE FULLTIME; FORMAT RACE NEWRACE.; VAR NUMKIDS; TABLE FULLTIME, RACE=' '*NUMKIDS=' '*(N*F=6. MEAN*F=5.1)/ BOX='Number of Children' RTS=12; RUN; /* Output 13.6 */ PROC TABULATE DATA=TEMP; CLASS RACE FULLTIME; FORMAT RACE NEWRACE.; VAR INCOME; TABLE FULLTIME, RACE=' '*INCOME=' '*(N*F=4. MEAN*F=DOLLAR5.) / BOX='Income' RTS=12; RUN; /* Output 13.7 */ PROC TABULATE DATA=TEMP; CLASS RACE FULLTIME; FORMAT RACE NEWRACE.; VAR INCOME; TABLE FULLTIME, RACE=' '*INCOME=' '*(N*F=6. MEAN*F=DOLLAR8.) / BOX='Income' RTS=12; RUN; /* Reformatting the column variables*/ /* Output 13.8 */ PROC TABULATE DATA=TEMP FORMAT=6.1; CLASS RACE OCCUP GENDER; FORMAT RACE NEWRACE.; VAR NUMKIDS; TABLE RACE, GENDER=' '*OCCUP=' '*NUMKIDS=' '*(MEAN) / BOX='Number of Children' RTS=12; RUN; /* Output 13.9 */ PROC FORMAT; VALUE OCC1FT 1="Manag-ement" 2="Profes-sional" 3="Tech-nical" 4="Sales"; VALUE OCC2FT 1="Mgmt." 2="Prof." 3="Tech." 4="Sales"; RUN; PROC TABULATE DATA=TEMP FORMAT=6.1; CLASS RACE OCCUP GENDER; FORMAT RACE NEWRACE. OCCUP OCC1FT.; VAR NUMKIDS; TABLE RACE, GENDER=' '*OCCUP=' '*NUMKIDS=' '*(MEAN) / BOX='Number of Children' RTS=12; RUN; /* Output 13.10 */ PROC TABULATE DATA=TEMP FORMAT=6.1; CLASS RACE OCCUP GENDER; FORMAT RACE NEWRACE. OCCUP OCC2FT.; VAR NUMKIDS; TABLE RACE, GENDER=' '*OCCUP=' '*NUMKIDS=' '*(MEAN) / BOX='Number of Children' RTS=12; RUN;/* Moving the statistics*/ /* Output 13.11 */ PROC TABULATE DATA=TEMP; CLASS OCCUP FULLTIME; VAR INCOME; TABLE FULLTIME, INCOME*OCCUP=' '* (N*F=5. MEAN*F=DOLLAR8. STD*F=DOLLAR8.) / RTS=12; RUN; /* Output 13.12 */ PROC TABULATE DATA=TEMP; CLASS OCCUP FULLTIME; VAR INCOME; TABLE FULLTIME*(N*F=5. MEAN*F=DOLLAR8. STD*F=DOLLAR8.), INCOME*OCCUP=' ' / RTS=12; RUN; /* Output 13.13 */ PROC TABULATE DATA=TEMP; CLASS OCCUP FULLTIME; VAR INCOME; TABLE FULLTIME*(N*F=12. MEAN*F=DOLLAR12. STD*F=DOLLAR12.), INCOME*OCCUP=' ' / RTS=24; RUN; /* Rotating the table*/ /* Output 13.14 */ OPTIONS LS=132 PS=50; PROC TABULATE DATA=TEMP; CLASS RACE OCCUP GENDER; FORMAT RACE NEWRACE.; VAR NUMKIDS; TABLE RACE, GENDER=" "*OCCUP=" "*NUMKIDS=" "*MEAN=" " / BOX="Number of Children" RTS=12; RUN; /* Output 13.15 */ PROC TABULATE DATA=TEMP; CLASS RACE OCCUP GENDER; FORMAT RACE NEWRACE.; VAR NUMKIDS; TABLE GENDER=" "*OCCUP=" "*NUMKIDS=" "*MEAN=" ", RACE / BOX="Number of Children" RTS=26 ROW=FLOAT; RUN; /* Removing a heading by putting it in the table BOX*/ DATA TEMP; SET CHPT13; YEAR=YEAR+2; IF YEAR IN(1987,1992) THEN YEAR=1995; ELSE IF YEAR=1988 THEN YEAR=1996; ELSE IF YEAR=1990 THEN YEAR=1997; RUN; /* Output 13.16 */ OPTIONS PS=47;PROC TABULATE DATA=TEMP; CLASS YEAR GENDER OCCUP; FORMAT RACE NEWRACE.; VAR INCOME; TABLE YEAR*(GENDER ALL), OCCUP=" "*INCOME*(N*F=5. MEAN*F=DOLLAR8.); RUN; /* Output not shown in book */ PROC TABULATE DATA=TEMP; CLASS YEAR GENDER OCCUP; FORMAT RACE NEWRACE.; VAR INCOME; TABLE YEAR*(GENDER ALL), OCCUP=" "*INCOME=" "*(N*F=5. MEAN*F=DOLLAR8.) / BOX="Income"; RUN; /* Removing the row separators*/ DATA TEMP; SET CHPT13A; RUN; /* Output 13.17 */ PROC TABULATE DATA=TEMP NOSEPS; CLASS YEAR AGE; VAR SAT; TABLES YEAR=" ", SAT=" "*AGE="Age Group"*MEAN=" "*F=6.2 / RTS=15 BOX="Mean Satisfaction Score"; RUN; /* Output 13.18 */ DATA TEMP2; SET TEMP; DECNUM=FLOOR(YEAR/10)*10; DECADE=DECNUM||"'s"; RUN; PROC TABULATE DATA=TEMP2 NOSEPS; CLASS YEAR AGE DECADE; VAR SAT; TABLES DECADE=' '*YEAR=' ', SAT=' '*AGE='Age Group'*MEAN=' '*F=6.2 / RTS=15 INDENT=3 BOX='Mean Satisfaction Score'; RUN; /* Changing the font size*/ /* Output 13.19 */ DATA TEMP; SET CHPT13; RUN; PROC TABULATE DATA=TEMP; CLASS OCCUP RACE GENDER; VAR INCOME; TABLE RACE=" "*(GENDER=" " ALL), OCCUP=" "*INCOME=" "*(N*F=8. MEAN*F=DOLLAR10.) / BOX="Income" ROW=FLOAT; RUN;/* Output 13.20 */ /* Note: this example will only work if you license SAS\GRAPH */ FILENAME LISFILE "C:\TEMP\TABOUT.LIS"; PROC PRINTTO PRINT=LISFILE NEW; RUN; PROC TABULATE DATA=TEMP; CLASS OCCUP RACE GENDER; VAR INCOME; TABLE RACE=" "*(GENDER=" " ALL), OCCUP=" "*INCOME=" "*(N*F=8. MEAN*F=DOLLAR10.) / BOX="Income" ROW=FLOAT; RUN; PROC PRINTTO; RUN; GOPTIONS HTEXT=.8 DEVICE=WIN; PROC GPRINT FILEREF=LISFILE; RUN; /* Export table to spreadsheet*/ /* Output 13.21 */ FILENAME LISFILE 'C:\TEMP\TABOUT.LIS'; PROC PRINTTO PRINT=LISFILE NEW; RUN; OPTIONS PS=250 LS=250 NOCENTER; PROC TABULATE DATA=TEMP NOSEPS FORMCHAR=', CLASS OCCUP RACE GENDER; VAR INCOME; TABLE RACE=' '*(GENDER=' ' ALL), OCCUP=' '*INCOME=' '*MEAN=' '*F=14. / BOX='Mean Income' ROW=FLOAT RTS=30; RUN; PROC PRINTTO; RUN; /* Splitting up the table*/ /* Output 13.23 */ OPTIONS LS=90; PROC TABULATE DATA=TEMP; CLASS OCCUP RACE GENDER FULLTIME; FORMAT RACE NEWRACE.; VAR INCOME; TABLE RACE=" "*(GENDER=" " ALL), FULLTIME=" "*OCCUP=" "*INCOME=" "*(N*F=4. MEAN*F=DOLLAR8.) / BOX="Income" RTS=21 ROW=FLOAT; RUN; /* Output 13.24 */ PROC TABULATE DATA=TEMP; CLASS OCCUP RACE GENDER FULLTIME; FORMAT RACE NEWRACE.; VAR INCOME; TABLE FULLTIME=" ", RACE=" "*(GENDER=" " ALL), OCCUP=" "*INCOME=" "*(N*F=4. MEAN*F=DOLLAR8.) / BOX="Income" RTS=21 ROW=FLOAT; RUN; ';/**********************************************************/ /* Chapter 14: Using Macros to Generate Tables */ /**********************************************************/ /* Repeating a table for a series of class variables*/ OPTIONS MPRINT SYMBOLGEN; DATA TEMP; SET CHPT14; RUN; /* Output 14.1 */ PROC TABULATE DATA=TEMP; CLASS SECTOR FULLTIME GENDER; VAR NUMEMP; TABLE SECTOR, FULLTIME=' '*GENDER=' ' *NUMEMP=' '*MEAN=' '*F=8. / BOX="Mean Number of Employees"; TITLE "Original Table Showing Company Data"; RUN; /* Output 14.2 */ %MACRO TABLEIT(ROWVAR); PROC TABULATE DATA=TEMP; CLASS FULLTIME GENDER &ROWVAR; VAR NUMEMP; TABLE &ROWVAR, FULLTIME=' '*GENDER=' ' *NUMEMP=' '*MEAN=' '*F=8. / BOX="Mean Number of Employees"; TITLE "Macro-Generated Table Showing Company Data"; RUN; %MEND TABLEIT; %TABLEIT(SECTOR); %TABLEIT(UNION); /* Repeating a table for a series of statistics*/ /* Output not shown */ PROC TABULATE DATA=TEMP; CLASS SECTOR FULLTIME GENDER; VAR NUMEMP; TABLE SECTOR, FULLTIME=' '*GENDER=' ' *NUMEMP=' '*MEAN=' '*F=8. / BOX="Mean Number of Employess"; TITLE "Original Table Showing Company Data"; RUN; %MACRO TABLEIT(THESTAT, THELABEL, THEFMT); PROC TABULATE DATA=TEMP; class sector fulltime gender; var numemp; table sector, fulltime=' '*gender=' ' *numemp=' '*(&THESTAT)*F=&THEFMT / box="&THELABEL" RTS=20; title "GENERIC TABLE SHOWING COMPANY DATA"; RUN; %MEND TABLEIT;/* Output 14.3, 14.4, 14.5 */ %MACRO TABLEIT(THESTAT, THELABEL, THEFMT); PROC TABULATE DATA=TEMP; CLASS FULLTIME GENDER SECTOR; VAR NUMEMP; TABLE SECTOR, FULLTIME=' '*GENDER=' ' *NUMEMP=' '*(&THESTAT)*F=&THEFMT / BOX="&THELABEL"; TITLE "Macro-Generated Table Showing Company Data"; RUN; %MEND TABLEIT; %TABLEIT(MEAN, Mean Number of Employees, 8.); %TABLEIT(MEAN STD,Number of Employees (Mean and Std), 8.2); %TABLEIT(PCTN,Percent Employees by Sector, 8.1); /* Repeating a table for various time periods*/ /* Output 14.6 */ PROC TABULATE DATA=TEMP; CLASS UNION FULLTIME; VAR INCOME; TABLE UNION=' ', FULLTIME=' '*INCOME=' '*MEAN=' '; TITLE "Mean Income (All Observations)"; RUN; /* Output 14.7 */ %MACRO TABLEIT(WHERECL,THELABEL); PROC TABULATE DATA=TEMP; WHERE &WHERECL; CLASS UNION FULLTIME; VAR INCOME; TABLE UNION=' ', FULLTIME=' '*INCOME=' '*MEAN=' '; TITLE "Mean Income (&THELABEL)"; RUN; %MEND TABLEIT; %TABLEIT('1JAN97'D11 THEN OCCUP=11; RUN; /* Output 15.18 */ PROC FORMAT; VALUE FOOTFT 1='Managerial' 2='Professional' 3='Technical' 4='Sales' 5='Clerical' 6-8='Services' 9-10='Manufacturing' 11='Farming**'; RUN; FOOTNOTE 'Source: XYZ Company Survey.'; FOOTNOTE2 '* Part-time includes employees working