2017-05-09 151 views
0

我需要做這個列數據的頻率分佈而不使用任何proc freq; proc sql。我只允許使用proc排序。SAS countif函數不使用proc

在Excel中,我會使用一個簡單的countif,但我不知道如何在SAS給出以上的限制。

data sample_grades; 
input grades $; 
datalines; 
C 
A 
A 
B 
B+ 
A- 
W 
A 
A- 
A 
A- 
A 
B+ 
A- 
A 
B+ 
B+ 
A- 
B+ 
; 
run; 

我想出了這一點,但它停在A-

data new_dataset; 
set Fall2016; 
by grade; 
retain grade frequency; 
if grade = 'A' then frequency+1; 
else if grade = 'A-' then frequency=0; 
if grade = 'A-' then frequency+1; 
else if grade = 'B' then frequency=0; 
if grade = 'B' then frequency+1; 
else if grade = 'B+' then frequency=0; 
if grade = 'B+' then frequency+1; 
else if grade = 'B-' then frequency=0; 
if grade = 'B-' then frequency+1; 
else if grade = 'C' then frequency=0; 
if grade = 'C' then frequency+1; 
else if grade = 'W' then frequency=0; 
if grade = 'W' then frequency+1; 
else frequency+0; 
if last.grade then do; 
frequency+0; 
end; 
run; 

最終計數我在找一個簡單的表是這樣的: enter image description here

回答

1

它有助於想到數據作爲循環的步驟,它貫穿輸入數據集並隨着時間的推移而拾取值。我正要解釋你在這方面的嘗試,但很快就變得令人困惑。這裏是我的問題的嘗試:

data sample_grades; 
input grades $; 
datalines; 
C 
A 
A 
B 
B+ 
A- 
W 
A 
A- 
A 
A- 
A 
B+ 
A- 
A 
B+ 
B+ 
A- 
B+ 
; 
run; 

排序第一的成績數據,讓BY-GROUP處理可能發生:

proc sort data=sample_grades; 
    by grades; 
run; 

現在設置你的數據的步驟如下:

data new_dataset; 
    set sample_grades; 
    by grades; 
    /* If it's the first of the grades then set the frequency to zero */ 
    if first.grades then frequency=0; 
    /* Increment the frequency value regardless of the value of grades */ 
    frequency+1; 
    /* When the last of the grades values is found, output. This gives the total frequency for the grade in the output table */ 
    if last.grades then output; 
run;