SAS:在数据步骤中,如何计算观察子集的总均值,跳过缺失值

SAS: in the DATA step, how to calculate the grand mean of a subset of observations, skipping over missing values

我正在尝试在 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;