Getting Started Examples for PROC SURVEYFREQ

/****************************************************************/
/*          S A S   S A M P L E   L I B R A R Y                 */
/*                                                              */
/*    NAME: SURFGS                                              */
/*   TITLE: Getting Started Examples for PROC SURVEYFREQ        */
/* PRODUCT: STAT                                                */
/*  SYSTEM: ALL                                                 */
/*    KEYS: survey sampling, categorical data analysis,         */
/*    KEYS: stratification, clustering, unequal weighting,      */
/*    KEYS: frequency tables, crosstabulation tables,           */
/*    KEYS: Rao-Scott chi-square test, ODS Graphics             */
/*   PROCS: SURVEYFREQ                                          */
/*    DATA:                                                     */
/*                                                              */
/* SUPPORT: sasdlw                UPDATE: November 30, 2010     */
/*     REF: PROC SURVEYFREQ, Getting Started                    */
/*    MISC:                                                     */
/****************************************************************/
/* Generate Data -----------------------------------------------*/
proc format;
   value ResponseCode
      1 = 'Very Unsatisfied'
      2 = 'Unsatisfied'
      3 = 'Neutral'
      4 = 'Satisfied'
      5 = 'Very Satisfied';
run;

proc format;
   value UserCode
      1 = 'New Customer'
      0 = 'Renewal Customer';
run;

proc format;
   value SchoolCode
      1 = 'Middle School'
      2 = 'High School';
run;

proc format;
   value DeptCode
      0 = 'Faculty'
      1 = 'Admin/Guidance';
run;
data SIS_Survey;
   format Response ResponseCode.;
   format NewUser UserCode.;
   format SchoolType SchoolCode.;
   format Department DeptCode.;
   drop j;
   retain seed1 111;
   retain seed2 222;
   retain seed3 333;
   State = 'GA';
   NewUser = 1;
   do School=1 to 71;
      call rantbl( seed1, .45, .55, SchoolType );
      Department = 0;
      call rannor( seed3, x );
      SamplingWeight = 25 + x * 2;
      do j=1 to 2;
         if ( SchoolType = 1 ) then
            call rantbl( seed2, .16, .21, .30, .24, .09, Response);
         else
            call rantbl( seed2, .18, .23, .30, .22, .07, Response);
         output;
      end;
      output;
      Department = 1;
      call rannor( seed3, x );
      SamplingWeight = 15 + x * 1.5;
      do j=1 to 2;
         if ( SchoolType = 1 ) then
            call rantbl( seed2, .10, .15, .33, .28, .14, Response );
         else
            call rantbl( seed2, .13, .20, .30, .26, .11, Response);
         output;
      end;
   end;
   NewUser = 0;
   do School=72 to 134;
      call rantbl( seed1, .45, .55, SchoolType );
      Department = 0;
      call rannor( seed3, x );
      SamplingWeight = 25 + x * 2;
      do j=1 to 2;
         if ( SchoolType = 1 ) then
            call rantbl( seed2, .16, .21, .30, .24, .09, Response);
         else
            call rantbl( seed2, .18, .23, .30, .22, .07, Response);
         output;
      end;
      output;
      Department = 1;
      call rannor( seed3, x );
      SamplingWeight = 15 + x * 1.5;
      do j=1 to 2;
         if ( SchoolType = 1 ) then
            call rantbl( seed2, .10, .15, .33, .28, .14, Response );
         else
            call rantbl( seed2, .13, .20, .30, .26, .11, Response);
         output;
      end;
   end;
   State = 'NC';
   NewUser = 1;
   do School = 135 to 218;
      call rantbl( seed1, .45, .55, SchoolType );
      Department = 0;
      call rannor( seed3, x );
      SamplingWeight = 25 + x * 2;
      if ( SchoolType = 1 ) then
         call rantbl( seed2, .16, .21, .30, .24, .09, Response);
      else
         call rantbl( seed2, .18, .23, .30, .22, .07, Response);
      output;
      output;
      output;
      Department = 1;
      call rannor( seed3, x );
      SamplingWeight = 15 + x * 1.5;
      if ( SchoolType = 1 ) then
         call rantbl( seed2, .10, .15, .33, .28, .14, Response );
      else
         call rantbl( seed2, .13, .20, .30, .26, .11, Response);
      output;
      output;
   end;
   NewUser = 0;
   do School = 219 to 274;
      call rantbl( seed1, .45, .55, SchoolType );
      Department = 0;
      call rannor( seed3, x );
      SamplingWeight = 25 + x * 2;
      do j=1 to 2;
         if ( SchoolType = 1 ) then
            call rantbl( seed2, .16, .21, .30, .24, .09, Response);
         else
            call rantbl( seed2, .18, .23, .30, .22, .07, Response);
         output;
      end;
      output;
      Department = 1;
      call rannor( seed3, x );
      SamplingWeight = 15 + x * 1.5;
      if ( SchoolType = 1 ) then
         call rantbl( seed2, .10, .15, .33, .28, .14, Response );
      else
         call rantbl( seed2, .13, .20, .30, .26, .11, Response);
      output;
      output;
   end;
   State = 'SC';
   NewUser = 1;
   do School = 275 to 328;
      call rantbl( seed1, .45, .55, SchoolType );
      Department = 0;
      call rannor( seed3, x );
      SamplingWeight = 25 + x * 2;
      do j=1 to 2;
         if ( SchoolType = 1 ) then
            call rantbl( seed2, .16, .21, .30, .24, .09, Response);
         else
            call rantbl( seed2, .18, .23, .30, .22, .07, Response);
         output;
      end;
      output;
      Department = 1;
      call rannor( seed3, x );
      SamplingWeight = 15 + x * 1.5;
      if ( SchoolType = 1 ) then
         call rantbl( seed2, .10, .15, .33, .28, .14, Response );
      else
         call rantbl( seed2, .13, .20, .30, .26, .11, Response);
      output;
      output;
   end;
   NewUser = 0;
   do School = 329 to 370;
      call rantbl( seed1, .45, .55, SchoolType );
      Department = 0;
      call rannor( seed3, x );
      SamplingWeight = 25 + x * 2;
      do j=1 to 2;
         if ( SchoolType = 1 ) then
            call rantbl( seed2, .16, .21, .30, .24, .09, Response);
         else
            call rantbl( seed2, .18, .23, .30, .22, .07, Response);
         output;
      end;
      output;
      Department = 1;
      call rannor( seed3, x );
      SamplingWeight = 15 + x * 1.5;
      if ( SchoolType = 1 ) then
         call rantbl( seed2, .10, .15, .33, .28, .14, Response );
      else
         call rantbl( seed2, .13, .20, .30, .26, .11, Response);
      output;
      output;
   end;
run;
/* One-Way Frequency Table -------------------------------------*/
title 'Student Information System Survey';
proc surveyfreq data=SIS_Survey;
   tables  Response;
   strata  State NewUser;
   cluster School;
   weight  SamplingWeight;
run;
/* One-Way Frequency Table
      Confidence Limits for Percentages
      Rao-Scott Chi-Square Goodness-of-Fit Test ----------------*/
ods graphics on;
proc surveyfreq data=SIS_Survey nosummary;
   tables  Response / clwt nopct chisq
                      plots=WtFreqPlot;
   strata  State  NewUser;
   cluster School;
   weight  SamplingWeight;
run;
/* Two-Way Crosstabulation Table -------------------------------*/
proc surveyfreq data=SIS_Survey nosummary;
   tables  SchoolType * Response /
      plots=wtfreqplot(type=dot scale=percent groupby=row);
   strata  State NewUser;
   cluster School;
   weight  SamplingWeight;
run;
ods graphics off;
/* Two-Way Crosstabulation Table
      Row Percentages
      Rao-Scott Chi-Square Test  -------------------------------*/
proc surveyfreq data=SIS_Survey nosummary;
   tables  SchoolType * Response / row nowt chisq;
   strata  State NewUser;
   cluster School;
   weight  SamplingWeight;
run;