楼主: Tigflanker
1217 5

[问答] 问一个观测紧密排列的问题 [推广有奖]

  • 8关注
  • 18粉丝

副教授

49%

还不是VIP/贵宾

-

威望
0
论坛币
2321 个
通用积分
9.9128
学术水平
179 点
热心指数
194 点
信用等级
167 点
经验
27443 点
帖子
622
精华
0
在线时间
851 小时
注册时间
2011-3-27
最后登录
2023-5-14

楼主
Tigflanker 发表于 2013-3-26 20:57:28 来自手机 |AI写论文
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 ...
关键词:print 数据集 int 缺失值

沙发
yongyitian 发表于 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;
已有 3 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
webgu + 60 + 20 热心帮助其他会员
Tigflanker + 1 + 1 + 1 观点有启发
a6566792 + 1 + 1 + 1 观点有启发

总评分: 经验 + 60  论坛币 + 20  学术水平 + 2  热心指数 + 2  信用等级 + 2   查看全部评分

藤椅
Tigflanker 发表于 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

怪我没把问题考虑清楚,如果这个连续问题到晚上还没解决,我采纳楼上仁兄的回答。
已有 1 人评分经验 论坛币 收起 理由
webgu + 80 + 10 热心帮助其他会员

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

Bye SAS.
若有缘,能重聚。

板凳
yongyitian 发表于 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.

报纸
yongyitian 发表于 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 的结果。

地板
Tigflanker 发表于 2013-3-27 11:37:18
已经用set,missing,merge解决啦;谢谢大家
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
yongyitian + 1 + 1 + 1 观点有启发

总评分: 学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

Bye SAS.
若有缘,能重聚。

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-29 22:30