Sas 当我使用Proc Freq时,我想显示频率为零的观测值

Sas 当我使用Proc Freq时,我想显示频率为零的观测值,sas,Sas,我试图对考试成绩进行频率分析。 我的数据集有记录分数的学生 如下图所示: student, score 1, 1 2, 1 3, 1 4, 3 5, 3 6, 3 7, 4 8, 4 9, 4 10, 4 我运行代码: proc freq data=stuff; var sco

我试图对考试成绩进行频率分析。 我的数据集有记录分数的学生

如下图所示:

student,  score

1,           1

2,           1

3,           1

4,           3

5,           3

6,           3

7,           4

8,           4

9,           4

10,          4
我运行代码:

proc freq data=stuff;
var score;
run;
输出:

score, freq, pct, cum.freq., cum.pct.

1,      3,    .3,    3,        .3

3,      3,    .3,    6,        .6

4,      4,    .4,    10,        1
我想展示:

score, freq, pct, cum.freq., cum.pct.

1,      3,    .3,    3,        .3

2,      0,     0,    3,        .3

3,      3,    .3,    6,        .6

4,      4,    .4,    10,        1

5,      0,     0,    10,        1
SAS有没有办法做到这一点?
谢谢你的帮助。

总有办法的。这里有一些方法。它们要么涉及手工构建表格,要么在要求SAS进行总结之前对输入进行预处理

这些将作为以下方法的输入

这种方法将虚拟值添加到原始数据中,并使用proc freq的权重函数来决定哪些行应该参与计数。我更喜欢这种方法

/* Combine the inputs and add weight variable */
data input;
    set 
        have (in = a) 
        numbers;
    /* Set weight so that empty groups won't be counted */
    weight = ifn(a, 1, 0)
run;
/* Create the report */
proc freq data = input;
    table score;
    /* Only add to the count when weight = 1, include 0 rows */
    weight weight / zeros;
run;
这种方法手工构建表格

这种方法修改proc freq输出以添加额外的行

使用上面Joe的建议也会让您获得部分帮助,但不幸的是,无法计算累积列。 这是使用Joe方法的部分解


您能否格式化数据以更清楚地显示输出和输入?我会这样做,但我不确定,因为你的第一个分数1只显示了3个计数,但你的样本数据中有4个。我没有时间完全回答,但如果其他人没有研究PrelofMT选项。你仍然需要提前知道所有的等级,所以我不太喜欢这种方法,但是如果数据中不存在这种方法,计算机怎么知道它存在呢?标记的副本很好地回答了这一点,并解释了Reeza提到的方法。我认为对于上述5个级别的适当情况,这是有意义的;对于具有多个级别的情况,您可以从数据或编程方法中创建格式。@Joe,不幸的是,我不认为这与proc TABLATE一样是重复的。如果这很重要,我不介意重新打开,但从问题来看,这一点并不明显重要。问题只是显示proc freq的默认输出。
/* Combine the inputs and add weight variable */
data input;
    set 
        have (in = a) 
        numbers;
    /* Set weight so that empty groups won't be counted */
    weight = ifn(a, 1, 0)
run;
/* Create the report */
proc freq data = input;
    table score;
    /* Only add to the count when weight = 1, include 0 rows */
    weight weight / zeros;
run;
/* Get the total count and each group's count */
proc summary data = have;
    class score;
    output out = counts;
run;
/* Combine and create the summary stats */
data want;
    merge 
        counts (rename = (_FREQ_ = freq)) 
        numbers;
    by score;
    /* Keep the total and cumulative total when new rows are loaded */
    retain total cumsum;
    /* Set up the total and delete the total row */
    if _TYPE_ = 0 then do; 
        total = freq;
        delete;
    end;
    /* Prevent missing values by adding 0 */
    freq + 0;
    /* Calculate stats */
    pct = freq / total; 
    cumsum + freq;
    cumpct = cumsum / total;
    drop _TYPE_ total;
run;
proc print;run;
/* Create the frequency report as a dataset */
proc freq data = have;
    table score;
    /* Output as dataset */
    ods output OneWayFreqs = freqs (drop = table f_score);
run;
/* Combine and build the extra rows */
data want (drop = _:);
    merge 
        freqs (in = f)
        numbers;
    by score;
    /* Set up temporary variables for storing cumulatives */
    retain _lp _lf;
    /* Store cumulatives */
    if f then do;
        _lf = CumFrequency;
        _lp = CumPercent;
    end;
    /* Put stored values into new rows */
    else do;
        Frequency = 0;
        Percent = 0;
        CumFrequency = _lf;
        CumPercent = _lp;
    end;
run;
proc print; run;
/* Create a format showing each of the desired levles */
proc format;
    value score
        1='1'
        2='2'
        3='3'
        4='4'
        5='5';
quit;
proc tabulate data = have;
    /* Specify that every formatted value should be included */
    class score / preloadfmt;
    /* Apply the format */
    format score score.;
    tables score, 
        /* Request the output table include frequency and percent */
        (n="freq" pctn="cumfreq" ) / 
        /* Include missings and display them as 0s */
        printmiss misstext="0";
run;