全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
1169 5
2013-03-26
悬赏 50 个论坛币 已解决
呵呵,来问个问题,是这样的,假设我的数据集如下:
a b
1 .
. 2
3 4
能否有什么办法,让每一列变量的观测没有缺失值(全部挤压上去)
a b
1 2
3 4

我的最终目的是想print紧密的数据集,而不是像上面那种情况,因为如果是三个变量以上,就太占地方了.

谢谢.

最佳答案

yongyitian 查看完整内容

data have; input a b c; datalines; 1 . 2 . 3 4 6 7 . 8 9 10 11 12 13 ; proc transpose data=have out=have_trans; run; %macro mymacro(dsn=, out=); data &out; set &dsn; array col{5}; do i = 1 to dim(col); if col(i) = . then do; do j = i to dim(col)-1; col(j) = col(j+1); end; end; end; drop i j; run; %mend mymacro; %mymac ...
二维码

扫码加我 拉你入群

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

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

全部回复
2013-3-26 20:57:29
data have;
input a b c;
datalines;
1 . 2
. 3 4
6 7 .
8 9 10
11 12 13
;
proc transpose data=have out=have_trans;
run;

%macro mymacro(dsn=, out=);
data &out;
     set &dsn;
         array col{5};
         do i = 1 to dim(col);
               if col(i) = . then do;
                  do j = i to dim(col)-1;
                     col(j) = col(j+1);
                  end;
           end;
         end;
drop i j;
run;
%mend mymacro;
%mymacro(dsn=have_trans, out=have_out);

proc transpose data=have_out out=results (drop=_name_);
run;
二维码

扫码加我 拉你入群

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

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

2013-3-27 10:59:58
proc iml;
use out.case;
read all var _char_ into caseiml;
do i=1 to nrow(caseiml)-1;
  do j=1 to ncol(caseiml);
    if missing(caseiml[i,j]) then do;
          do k=1 to nrow(caseiml);
            if not missing(caseiml[i+k,j]) then do;
          caseiml[i,j]=caseiml[i+k,j];caseiml[i+k,j]="";
                end;
          end;
    end;
  end;
end;

create out.examm from caseiml;
append from caseiml;

quit;

我尝试用IML做了一下,没成功,关键有个问题,就是如果有连续缺失的就依然有问题了,例如

1 2
. 3
. 4
5 .
6 7

怪我没把问题考虑清楚,如果这个连续问题到晚上还没解决,我采纳楼上仁兄的回答。
二维码

扫码加我 拉你入群

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

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

2013-3-27 11:16:04
submit the macro again
%mymacro(dsn=have_out, out=have_out_1);

then run the proc transform use the new result dataset have_out_1.

If there are five 连续 missing, then submit the macro five times with the same input and output dataset name. To see the result of each time you submit the macro,  you can change the input and output dataset every time as above.

二维码

扫码加我 拉你入群

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

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

2013-3-27 11:25:12
Tigflanker 发表于 2013-3-27 10:59
proc iml;
use out.case;
read all var _char_ into caseiml;
忘了说, 所有的变量必须是数值变量。有_character_ 好象不行。你可以看一下 transpose 的结果。
二维码

扫码加我 拉你入群

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

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

2013-3-27 11:37:18
已经用set,missing,merge解决啦;谢谢大家
二维码

扫码加我 拉你入群

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

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

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群