楼主: yzzhang
5514 20

[原创博文] 请教:如何将一个数据集的所有变量置为缺失? [推广有奖]

11
soporaeternus 发表于 2010-9-29 16:45:44
感觉array来做有点不好控制,没办法,比较菜......
偷懒用宏了
  1. proc sql;
  2. select compress(name||"="||case when upcase(type)="CHAR" then "''" else "." end)  into :a separated by ";"  
  3. from sashelp.vcolumn
  4. where upcase(libname)="WORK"
  5.   and upcase(memname)="B"
  6.   and upcase(name) not in ("Y");
  7. quit;
  8. %put "&a";
  9. data c;
  10.    set a;
  11.    set b;
  12.    if sum(x,y)>2 then do;
  13.     &a;
  14.    end;
  15. run;
复制代码
题外话:这个和左连接还是差别比较大的......
Let them be hard, but never unjust

12
yzzhang 发表于 2010-9-29 17:10:44
用宏的方法还是不错的。
期待能有更简介的方法!!

13
jingju11 发表于 2010-9-30 05:43:28
12# yzzhang

data c;
   set a;
   set b;
   if sum(x,y)>2 then call missing(of m--n);
run;
这种连接,如果变量名不重复的话,变量的顺序是不会混淆的。也就是说,左半边变量从a而来,右从b而来。如果要删掉b里的变量,只要定义首尾变量即可。不过我怀疑或许没有理解你的真实意图。

14
yzzhang 发表于 2010-9-30 09:15:23
当变量较多时,使用callmissing(of m-z)的形式,若数据集a中有变量y,则也会被置缺失。
其实可以把问题提炼为:怎么区分哪个变量来自哪个数据集的问题?!

13# jingju11

15
soporaeternus 发表于 2010-9-30 09:18:53
这个问题array有个方法就是
先set b;
然后array
再set a;
这样array里只有b的变量,然后vname去掉ID就可以了
Let them be hard, but never unjust

16
chouxiangdaishu 发表于 2010-9-30 16:06:28
觉得SAS语言,不是很规范。

17
yzzhang 发表于 2010-9-30 17:34:17
根据“soporaeternus”的提示终于用数组实现了上述功能。程序如下!^_^ 高兴,感谢大家,顺便帮看下有没有什么漏洞!
data a;
     input x a $ b $;
     datalines;
     1 a1 b1
     2 a2 b2
;
run;

data b;
     input y m $ n $;
     datalines;
     1 m1 n1
     2 m2 n2
;
run;

data c;
   set a;
                array a_character[*] _character_;
                array a_numeric[*] _numeric_;
   set b;
                array b_character[*] _character_;
                array b_numeric[*] _numeric_;
   if sum(x,y)>2 then do;
                do i=sum(dim(a_character,1),1) to dim(b_character,1);
                        b_character[i]='';
                end;
                do j=sum(dim(a_numeric,1),1) to dim(b_numeric,1);
                        b_numeric[j]=.;
                end;
        end;
        drop i j;
run;
已有 1 人评分学术水平 收起 理由
soporaeternus + 1 学习了!

总评分: 学术水平 + 1   查看全部评分

18
jingju11 发表于 2010-9-30 20:24:06
17# yzzhang

不理解。

19
soporaeternus 发表于 2010-9-30 21:47:27
18# jingju11
后定义的array里有两个dataset里所有的变量,
即前array有N(a)的变量数
后array有N(a)+N(b)个变量数
即从N(a)+1开始遍历后一个array就是b的所有变量

呵呵,很好的想法,学习了,谢谢
Let them be hard, but never unjust

20
jingju11 发表于 2010-10-1 05:04:08
yzzhang 发表于 2010-9-30 09:15
当变量较多时,使用callmissing(of m-z)的形式,若数据集a中有变量y,则也会被置缺失。
其实可以把问题提炼为:怎么区分哪个变量来自哪个数据集的问题?!

13# jingju11
为什么?,如果数据a和b有相同的变量,应该是b里的自动跑到a里了,也就是替换。准确的说,set;set; 不就是1-1 merge并取较短的观测数吗?

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-3 01:12