SAS:proc hpbin函数
我掌握的数据是SAS:proc hpbin函数,sas,size,proc,bin,Sas,Size,Proc,Bin,我掌握的数据是 Year Score 2020 100 2020 45 2020 82 . . . 2020 91 2020 14 2020 35 我想要的结果是 Score_Ranking Count_Percent Cumulative_count_percent Sum top100 x y z 101-200 . . . 800-900 900-989 该数据集在同一年共有989次观测
Year Score
2020 100
2020 45
2020 82
.
.
.
2020 91
2020 14
2020 35
我想要的结果是
Score_Ranking Count_Percent Cumulative_count_percent Sum
top100 x y z
101-200
.
.
.
800-900
900-989
该数据集在同一年共有989次观测。我想将整个数据集分成10个存储箱,但将大小设置为100。但是,如果使用proc hpbin函数,结果将被划分为989/10个存储箱。有没有办法确定垃圾箱的大小
此外,我还需要显示比例、累计比例和分数总和的其他行。我怎样才能把这些打印在箱子旁边
先谢谢你
*generate fake data;
data have;
do score=1 to 998;
output;
end;
run;
proc sort data=have;
by score;
run;
方法#1
请注意,我在这里使用的是一个视图,而不是一个数据集,如果效率可能是一个问题,它会有所帮助
*create bins;
data binned / view=binned;
set have ;
if mod(_n_, 100) = 1 then bin+1;
run;
*calculate counts/percentages;
proc freq data=binned noprint;
table bin / out=binned_counts outcum;
run;
*calculate sums - not addition of WEIGHT;
proc freq data=binned noprint;
table bin / out=binned_sum outcum;
weight score;
run;
*merge results together;
data want_merged;
merge binned_counts binned_sum (keep = bin count rename = count= sum);
by bin;
run;
方法#2
还有另一种方法,它需要单次传递数据,而不是像PROC-FREQ方法那样多次传递:
*manual approach;
data want;
set have
nobs = _nobs /*Total number of observations in data set*/
End=last /*flag for last record*/;
*holds values across rows and sets initial value;
retain bin 1 count cum_count cum_sum 0 percent cum_percent ;
*increments bins and resets count at start of each 100;
if mod(_n_, 100) = 1 and _n_ ne 1 then do;
*output only when end of bin;
output;
bin+1;
count=0;
end;
*increment counters and calculate percents;
count+1;
percent = count / _nobs;
cum_count + 1;
cum_percent = cum_count / _nobs;
cum_sum + score;
*output last record/final stats;
if last then output;
*format percents;
format percent cum_percent percent12.1;
run;
由于您没有使用任何类型的自动装箱,我认为您不需要任何程序。您考虑了一个算法,因此可以使用数据步骤+过程手动应用该算法。我会使用一个格式和proc freq。