楼主: wanfurui
5978 8

SAS编写用平均值替代缺失值 [推广有奖]

  • 0关注
  • 0粉丝

VIP

本科生

0%

还不是VIP/贵宾

-

威望
0
论坛币
733 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
708 点
帖子
53
精华
0
在线时间
72 小时
注册时间
2010-2-17
最后登录
2020-11-1

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
编制程序:
3个变量d1-d3,数据如下:
1      2       7
2      3       .
3      .         4
要求:如果发现缺失值,即用该行数据的平均值代替,输出结果如下:
1       2       7
2       3       2.5
3       3.5     4
我的程序如下:
data;
input d1-d3;
array s d1-d3;
total=d1+d2+d3;
do i=1 to 3;
if s=.  then s=total/3;
end;
cards;
1      2       7  
2      3       .
3      .         4
;
run;
proc print;
run;
但是结果出不来,求高手请教!
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:平均值 缺失值 Total Input cards SAS 缺失 编写 平均值

回帖推荐

windlove 发表于9楼  查看完整内容

To use the row mean: data test; input x1-x3; array x{*} x1-x3; mean = mean(of x1-x3); do i = 1 to dim(x); if x=. then x=mean; end; drop mean i; datalines; 1 2 7 2 3 . 3 . 4 ; proc print;run; To use the column mean; data test; input x1-x3; datalines; 1 2 7 2 3 . 3 . 4 ; proc means noprint; output out=test2(drop=_type_ _freq_) mean(x1 x2 x3) = d1 d2 d3 ; data fin ...

bobguy 发表于5楼  查看完整内容

SAS has many statistic function you can use with a array or a variable list. Here is an example. data; input d1-d3; array s(*) d1-d3; n=n(of s(*)); mean=mean(of s(*)); sum=sum( of s(*)); do i=1 to dim(s); if s(i)=. then s(i)=mean; end; drop i; cards; 1 2 7 2 3 . 3 . 4 ; run; proc print; run;

本帖被以下文库推荐

沙发
crackman 发表于 2010-3-6 19:21:11 |只看作者 |坛友微信交流群
total=d1+d2+d3;
计算的是非缺失变量的和
既然D2 D3都有缺失
就不能那样计算了

使用道具

藤椅
crackman 发表于 2010-3-6 20:02:26 |只看作者 |坛友微信交流群
data test(drop=i);
input d1-d3@;
array s{3} d1-d3;
do i=1 to 3;
if s(i)=. then d=i ;
end;
cards;
1      2       7  
2      .       4
3      4       .
;
run;
data a (drop=d);
set test;
if d=2 and d2=. then d2=(d1+d3)/2;
if d=3 and d3=. then d3=(d1+d2)/2;
proc print;
run;

使用道具

板凳
crackman 发表于 2010-3-6 20:03:50 |只看作者 |坛友微信交流群
只是一个抛砖引入的作用,希望有牛人来写出更好的程序

使用道具

报纸
bobguy 发表于 2010-3-6 22:10:30 |只看作者 |坛友微信交流群
crackman 发表于 2010-3-6 19:21
total=d1+d2+d3;
计算的是非缺失变量的和
既然D2 D3都有缺失
就不能那样计算了
SAS has many statistic function you can use with a array or a variable list.

Here is an example.

data;
input d1-d3;
array s(*) d1-d3;
n=n(of s(*));
mean=mean(of s(*));
sum=sum( of s(*));
do i=1 to dim(s);
  if s(i)=. then s(i)=mean;
end;
drop i;
cards;
1      2       7  
2      3       .
3      .         4
;
run;
proc print;
run;
已有 1 人评分经验 论坛币 收起 理由
bakoll + 3 + 3 精彩帖子

总评分: 经验 + 3  论坛币 + 3   查看全部评分

使用道具

地板
wanfurui 发表于 2010-3-6 22:27:56 |只看作者 |坛友微信交流群
第一部分定位缺失值的位置,很值得学习。谢谢!

使用道具

7
sdu0632 发表于 2010-3-7 00:41:21 |只看作者 |坛友微信交流群
What if I want it the following way?

Thanks,

1       2       7
2       3     5.5
3       2.5     4

使用道具

8
crackman 发表于 2010-3-7 09:30:33 |只看作者 |坛友微信交流群
5# bobguy
功底确实很深厚
谢谢

使用道具

9
windlove 发表于 2010-3-7 11:07:05 |只看作者 |坛友微信交流群
To use the row mean:
data test;
input x1-x3;
array x{*} x1-x3;
mean = mean(of x1-x3);
do i = 1 to dim(x);
  if x[i]=. then x[i]=mean;
end;
drop mean i;
datalines;
1 2 7
2 3 .
3 . 4
;
proc print;run;

To use the column mean;
data test;
input x1-x3;
datalines;
1 2 7
2 3 .
3 . 4
;
proc means noprint;
output out=test2(drop=_type_ _freq_)
       mean(x1 x2 x3) = d1 d2 d3 ;
data final;
merge test test2;
array x{*} x1-x3;
array d{*} d1-d3;
array dd{*} dd1-dd3;
retain dd1 dd2 dd3;
do i = 1 to 3;
if d[i]~=. then dd[i]=d[i];
   else dd[i] =dd[i];
if x[i] = . then x[i]=dd[i];
end;
drop d1-d3 dd1-dd3 i;

proc print; run;
已有 1 人评分经验 论坛币 收起 理由
bakoll + 3 + 3 精彩帖子

总评分: 经验 + 3  论坛币 + 3   查看全部评分

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-5-1 02:54