不使用 proc 的 SAS countif 函数
SAS countif function without using proc
我需要在不使用任何 proc freq 的情况下对这一列数据进行频率分布;过程 sql。我只被允许使用 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;
最终我想要一个像这样的简单 table:
enter image description here
将数据步骤视为循环会有所帮助,运行 通过输入数据集并在循环过程中获取值。我打算解释你的尝试在这方面是如何工作的,但很快就变得混乱了。这是我对这个问题的尝试:
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;
我需要在不使用任何 proc freq 的情况下对这一列数据进行频率分布;过程 sql。我只被允许使用 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;
最终我想要一个像这样的简单 table: enter image description here
将数据步骤视为循环会有所帮助,运行 通过输入数据集并在循环过程中获取值。我打算解释你的尝试在这方面是如何工作的,但很快就变得混乱了。这是我对这个问题的尝试:
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;