You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SAS-多变量卡方独立性检验代码需求:二分类变量关联分析

SAS Code for Batch Chi-Square Tests & Sorted Correlation Results

Got it, let's work through this SAS code request. You need to run chi-square independence tests between your binary variable v1 and over 200 categorical variables (v2 to v200+), exclude the ID column, then sort the results by chi-square statistic to find the variables most correlated with v1. Here's a solid, scalable solution:

Full SAS Code

/* --------------------------
   Step 1: Identify target variables
   -------------------------- */
/* Get list of all variables in your dataset */
proc contents data=your_data out=var_list noprint;
run;

/* Filter to keep only categorical variables (exclude ID and v1) */
/* Adjust TYPE=2 to TYPE=1 if your categorical variables are numeric */
data var_list;
    set var_list;
    where upcase(name) not in ('ID', 'V1') and type=2;
    keep name;
run;

/* Convert variable list to a macro variable for looping */
proc sql noprint;
    select name into :cat_vars separated by ' ' from var_list;
quit;

/* --------------------------
   Step 2: Prepare results dataset
   -------------------------- */
/* Create empty dataset to store chi-square test outputs */
data chi_results;
    length var_name $32 chi_square p_value df 8;
    stop; /* Stop immediately to create structure without rows */
run;

/* --------------------------
   Step 3: Macro to run batch chi-square tests
   -------------------------- */
%macro run_chi_tests;
    %let i = 1;
    %let current_var = %scan(&cat_vars., &i.);
    
    /* Loop through each categorical variable */
    %do %while(&current_var. ne);
        /* Run chi-square test between v1 and current variable */
        proc freq data=your_data noprint;
            tables v1 * &current_var. / chisq;
            output out=temp_results chisq; /* Export test stats to temp dataset */
        run;

        /* Clean up temp results and add variable name */
        data temp_results;
            set temp_results;
            var_name = "&current_var.";
            keep var_name chi_square p_value df;
        run;

        /* Append temp results to master results dataset */
        proc append base=chi_results data=temp_results;
        run;

        /* Move to next variable in the list */
        %let i = %eval(&i. + 1);
        %let current_var = %scan(&cat_vars., &i.);
    %end;
%mend run_chi_tests;

/* Execute the macro */
%run_chi_tests;

/* --------------------------
   Step 4: Sort and display results
   -------------------------- */
/* Sort results by chi-square statistic (descending = most correlated first) */
proc sort data=chi_results descending;
    by chi_square;
run;

/* Print the sorted results with clear labels */
proc print data=chi_results label noobs;
    label var_name = 'Categorical Variable'
          chi_square = 'Chi-Square Statistic'
          p_value = 'P-Value'
          df = 'Degrees of Freedom';
    title 'Top Variables Correlated with v1 (Sorted by Chi-Square)';
run;

Key Notes & Adjustments

  • Variable Type: The code assumes your categorical variables are character-type (type=2 in proc contents). If your categorical variables are numeric (e.g., coded 1/2/3), change type=2 to type=1 in the var_list data step.
  • ID Column: The code excludes ID (case-insensitive via upcase(name)). If your ID column has a different name, update the not in list.
  • Small Cell Counts: If any variable has expected cell counts <5, the chi-square test may be unreliable. You can add a check for this, or modify the tables statement to include exact fisher if you need Fisher's exact test instead for small samples.
  • Dataset Name: Replace your_data with the actual name of your input dataset.

How It Works

  1. Variable Identification: We first extract all relevant categorical variables (excluding ID and v1) to avoid manual listing of 200+ variables.
  2. Batch Testing: A macro loops through each variable, runs the chi-square test, and stores the key statistics (chi-square value, p-value, degrees of freedom) along with the variable name.
  3. Sorted Results: Finally, we sort the results by chi-square statistic in descending order so the variables most correlated with v1 appear at the top.

内容的提问来源于stack exchange,提问作者bartek09

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:34:52