我对以下问题有疑问。我的数据看起来像这样:
State Total
AZ 1000
AZ 1000
AZ -
CA -
CA 4000
也就是说,对于某些观察,我缺少变量“total”的数据。我想用非遗漏观察中的总数替换缺失值。
期望的输出
enter code here
State Total
AZ 1000
AZ 1000
AZ **1000**
CA **4000**
CA 4000
有什么想法吗?
答案 0 :(得分:1)
如果您的值是常量,请使用PROC STANDARDIZE替换缺失值。
Proc stdize data=have out=want missing=mean reponly;
By state;
Var amount;
Run;
答案 1 :(得分:0)
这是我提出的解决方案。当然有更优雅的方法来做到这一点,但这是经过测试和运作的。
Idea是对数据进行排序,以便缺少值在正确的值之后。然后循环每个州。保存'总计'从第一次观察到的值,并将其应用于该州任何遗失的细胞。
data begin;
length state $3 total 5;
input state Total;
cards;
AZ 1000 @@
AZ 1000 @@
AZ @@
CA @@
CA 4000 @@
OZ @@
OZ 3000 @@
OZ @@
;
run;
proc sort data=begin; by state descending total ; run;
data Filled;
set begin;
by state; /*Handle each state as own subset*/
retain memory; /*Keeps the 'memory' from prior observations and not from column */
if first.state then memory=total; /*Save the value to temporary column*/
if total=. then total=memory; /*Fill blanks*/
drop memory; /*Cleanup*/
run;
答案 2 :(得分:0)
用平均值合并。
proc sql;
select a.state,coalesce(a.total,b.total) from have a left join (select distinct state,mean(total) as total from have group by state) b on a.state=b.state;
quit;