楼主: riosen
7883 7

[原创博文] [求助]关于变量的删除 [推广有奖]

  • 2关注
  • 1粉丝

讲师

84%

还不是VIP/贵宾

-

威望
0
论坛币
778 个
通用积分
0
学术水平
0 点
热心指数
2 点
信用等级
0 点
经验
2498 点
帖子
162
精华
0
在线时间
1015 小时
注册时间
2007-1-16
最后登录
2014-5-5

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
<p>有N多个数据集,里面都有60多个变量,我要做的是:</p><p>当某个变量的前两个观测值为空值时,删除这个变量,请问有什么好的方法吗?</p><p>谢谢!</p>
二维码

扫码加我 拉你入群

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

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

关键词:多个变量 数据集 观测值

回帖推荐

kunkunred 发表于3楼  查看完整内容

There should be easy ways, but this is the best I can come up with./* Assume we start with data set Original and end up with Final *//* Generate data set containing only the first two observations */proc sql inobs=2;create table test as select *from original;quit;/* Use Proc Means to output the missing value of each variable */proc means data=test noprint;output out=test2 nmiss=/autoname;run;/* Tr ...

本帖被以下文库推荐

沙发
jlsstx 发表于 2009-4-13 20:24:00 |只看作者 |坛友微信交流群

也来看答案了

使用道具

藤椅
kunkunred 发表于 2009-4-13 22:40:00 |只看作者 |坛友微信交流群

There should be easy ways, but this is the best I can come up with.

/* Assume we start with data set Original and end up with Final */
/* Generate data set containing only the first two observations */
proc sql inobs=2;
create table test as
select *
from original;
quit;

/* Use Proc Means to output the missing value of each variable */
proc means data=test noprint;
output out=test2 nmiss=/autoname;
run;

/* Transpose the form of the data set to the form we can handle */
proc transpose data=test2(drop=_TYPE_ _FREQ_) out=test3(drop=Label) NAME=Source
 LABEL=Label PREFIX=MIS;
run;
/* Get rid of the '_NMiss' at the end of the variable name to recover the original variable name */
data test4;
set test3;
 Name=trim(tranwrd(source,'_NMiss',''));
run;

/* Put the variable names in a macro, which have one or none missing value*/
proc sql noprint;
select Name into:keep separated by ' '
from test4
where mis1<2;
quit;

/* If interested, you can look at the log to see which ones were kept */
%put &keep;
/* Clean up the room */
proc datasets nolist;
delete test test1 test2 test3 test4;
run;
/* Keep only the good ones :-)*/
data final;
set original(keep=&keep);
run;

[此贴子已经被作者于2009-4-13 22:47:46编辑过]

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

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

使用道具

板凳
yongyitian 发表于 2009-4-14 04:17:00 |只看作者 |坛友微信交流群

经测试三楼的程序可运行,结果正确。

使用道具

报纸
sasa1881 发表于 2010-3-4 21:33:25 |只看作者 |坛友微信交流群
3# kunkunred
作者程序是如果该变量有2个空值,则删除该变量,不是前两个值为空则删除该变量,如果这样做之前应该把其他变量的非空值处理下

使用道具

地板
sdu0632 发表于 2010-3-5 01:16:14 |只看作者 |坛友微信交流群
I know it looks stupid. :)


data test;
input x y z;
datalines;
. . 1
. . .
2 2 2
. 3 .
4 4 .
;
run;


data test1;
set test;
n+1;
x1=lag(x);
if (n=2 and x=. and x1=.) then x2="dele";
y1=lag(y);
if (n=2 and y=. and y1=.) then y2="dele";
z1=lag(z);
if (n=2 and z=. and z1=.) then z2="dele";
if n=2;
keep x2 y2 z2;
run;

使用道具

7
soporaeternus 发表于 2010-3-5 09:31:32 |只看作者 |坛友微信交流群
sasa1881 发表于 2010-3-4 21:33
3# kunkunred
作者程序是如果该变量有2个空值,则删除该变量,不是前两个值为空则删除该变量,如果这样做之前应该把其他变量的非空值处理下
就抽了前2条。
有2个空值=前两个值为空。

这个方法挺不错的,学习了
Let them be hard, but never unjust

使用道具

8
sasa1881 发表于 2010-3-5 20:06:58 |只看作者 |坛友微信交流群
失误,没注意到前面的sql...

使用道具

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

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

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

GMT+8, 2024-4-29 01:41