Published on :
Statistical CREATION_INTERNE

Test for a Difference Between Proportions (Chi-Square)

This code is also available in: Deutsch Español Français
Awaiting validation
The script begins by creating an internal dataset, 'practice1', via a DATA STEP and 'datalines'. This dataset contains soda (Coke/Pepsi) and pet (dog/cat) preferences, along with a weight for each combination. Then, it uses the PROC FREQ procedure with the CHISQ option to perform a chi-square test to determine if there is a statistically significant association between soda preference and pet preference. The 'weight weight;' option is used to apply the weights specified in the dataset. The 'expected' and 'nocol' options display the expected counts and suppress column percentages.
Data Analysis

Type : CREATION_INTERNE


Data is created directly within the script via a DATA STEP and the DATALINES statement. It includes soda and pet preferences, as well as a weighting variable.

1 Code Block
DATA STEP Data
Explanation :
This DATA STEP block creates the SAS dataset 'practice1'. It defines three variables: 'soda' (character), 'animal' (character), and 'weight' (numeric). The data is directly integrated into the script via the DATALINES statement, simulating observations of soda and pet preferences with their respective weights.
Copied!
1DATA practice1;
2 INPUT soda $ animal $ weight;
3DATALINES;
4Coke Dog 78
5Coke Cat 22
6Pepsi Dog 36
7Pepsi Cat 64
8;
9RUN;
2 Code Block
PROC FREQ
Explanation :
This PROC FREQ procedure analyzes the distribution of pet preferences by soda preference using the 'practice1' dataset. The 'tables soda*animal' statement creates a contingency table crossing these two variables. The 'chisq' option requests the calculation of the chi-square test for association. 'expected' displays the expected counts under the assumption of independence, and 'nocol' suppresses column percentages. The 'weight weight;' statement instructs the procedure to use the 'weight' variable as the frequency variable for calculations.
Copied!
1PROC FREQ DATA = practice1;
2 tables soda*animal / chisq expected nocol;
3 weight weight;
4RUN;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
Copyright Info : Practice: Test for a Difference Between Proportions This code is posted for your benefit; however, I highly recommend that you practice typing your own SAS programs as well. With the SAS programming language, as with all new languages, immersion seems to be the best way to learn.