我试图在data
步骤中计算观测值子集(例如,观测值20到观测值50)的总平均值。在这个计算中,我还想跳过(忽略)任何丢失的值。
我尝试过使用各种if … then
语句来处理mean
函数,但似乎无法将所有这些语句组合在一起。
任何帮助都将不胜感激。
作为参考,以下是我的数据步骤的基本概述:
data sas1;
infile '[file path]';
input v1 $ 1-9 v2 $ 11 v3 13-17 [redacted] RPQ 50-53 [redacted] v23 101-106;
v1=translate(v1,"0"," ");
format [redacted];
label [redacted];
run;
data gmean;
set sas1;
id=_N_;
if id = 10-40 then do;
avg = mean(RPQ);
end;
/*Here, I am trying to calculate the grand mean of the RPQ variable*/
/*but only observations 10 to 40, and skipping over missing values*/
run;
使用自动变量/_N_/来识别行。使用逐行保留的和值,然后除以最后的观察次数。使用missing()函数来确定存在的观测值的数量以及是否添加到运行总数中。
data stocks;
set sashelp.stocks;
retain sum_total n_count 0;
if 10<=_n_<=40 and not missing(open) then do;
n_count=n_count+1;
sum_total=sum_total+open;
end;
if _n_ = 40 then average=sum_total/n_count;
run;
proc print data=stocks(obs=40 firstobs=40);
var average;
run;
*check with proc means that the value is correct;
proc means data=sashelp.stocks (firstobs=10 obs=40) mean;
var open;
run;