楼主: susanzhu
1346 8

[问答] 逐个观测有条件的剔除变量 [推广有奖]

  • 0关注
  • 0粉丝

硕士生

37%

还不是VIP/贵宾

-

威望
0
论坛币
1347 个
通用积分
0.3604
学术水平
2 点
热心指数
3 点
信用等级
2 点
经验
1967 点
帖子
89
精华
0
在线时间
140 小时
注册时间
2007-11-6
最后登录
2024-4-22

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
现有的数据有点问题,所以需要有条件的剔除变量,比如
stkcd   费用1  费用2  费用3
1         管理    销售    其他
1          100   200      10
2         管理    其他    文件
2           20      50       70
3         其他   查看    文件
3          100    20        50

我想做的是比如stkcd=2时,如果观测等于‘其他’则费用3里面的文件就删掉,并且下面一行数字70页删掉。
觉得很难设计这个程序,不知道各位达人有没有办法,求助啊。
二维码

扫码加我 拉你入群

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

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

关键词:剔除变量 stkcd STK 有没有 不知道 程序

沙发
wwang111 发表于 2016-7-21 20:42:52 |只看作者 |坛友微信交流群
是这样吗?

data test;
input stkcd cost1 $ cost2 $ cost3 $;
  if stkcd ne lag(stkcd) then flag=0;
  if cost2="other" then flag+1;
  if flag=1 then call missing(cost3);
  drop flag;
cards;
1 admin sale other
1 100 200 10
2 admin other file
2 20 50 70
3 other check file
3 100 20 50
;

使用道具

藤椅
susanzhu 发表于 2016-7-21 20:49:00 |只看作者 |坛友微信交流群
wwang111 发表于 2016-7-21 20:42
是这样吗?

data test;
不好意思,可能我没说清楚,对于每一个stkcd,只要某一个cost等于'其他'则把这个观测和下一个观测中‘其他’后面所有变量都剔除。

使用道具

板凳
wwang111 发表于 2016-7-21 21:47:21 |只看作者 |坛友微信交流群
我暂时想到这个办法,有点麻烦,有可能走弯路了,你先看一下:

data test;
input stkcd cost1 $ cost2 $ cost3 $;
cards;
1 admin sale other
1 100 200 10
2 admin other file
2 20 50 70
3 other check file
3 100 20 50
;

%let costnum=3; /* number of COST variables*/
data _null_;
set test end=last;  
  length _code $100 code $1000;
   retain code;
   array cost cost:;
    do i=1 to dim(cost);
         if cost="other" then vname=vname(cost);
        end;
  if vname ne '' then num=input(compress(vname,,'a'),best.)+1;
  if .<num<=&costnum
    then _code='if stkcd='||cat(stkcd)||' then call missing(of cost'||cat(num)||"-cost&costnum);";
  if _n_=1 then code=_code;
  else code=strip(code)||strip(_code);
  if last then do;
  call execute('data wanted;set test;');
  call execute(code);
  call execute('run;');
  end;
run;

使用道具

报纸
susanzhu 发表于 2016-7-22 21:01:30 |只看作者 |坛友微信交流群
wwang111 发表于 2016-7-21 21:47
我暂时想到这个办法,有点麻烦,有可能走弯路了,你先看一下:

data test;
多谢你了, length _code 100code1000; 这行可以把变量定义那么长吗?
走弯路不要紧,能得到想要的结果才比较重要

使用道具

地板
wwang111 发表于 2016-7-22 21:06:16 来自手机 |只看作者 |坛友微信交流群
susanzhu 发表于 2016-7-22 21:01
多谢你了, length _code 100code1000; 这行可以把变量定义那么长吗?
走弯路不要紧,能得到想要的结果才 ...
我是怕你记录数太多,产生很多code,保险起见,长度只要够用就好,你可以根据具体数据调整

使用道具

7
susanzhu 发表于 2016-7-22 21:11:05 |只看作者 |坛友微信交流群
wwang111 发表于 2016-7-22 21:06
我是怕你记录数太多,产生很多code,保险起见,长度只要够用就好,你可以根据具体数据调整
我执行命令的时候报错说数值型变量的长度为3-8,后面的命令就没法执行了。
data re06;
set re05 end=last;  
  length _code 100code 2100;
   retain code;
   array item item:;
    do i=1 to dim(item);
         if item="其他" then vname=vname(item);
        end;
  if vname ne '' then num=input(compress(vname,,'a'),best.)+1;
  if .<num<=&itemnum
    then _code='if stkcd='||cat(stkcd)||' then call missing(of item'||cat(num)||"-item&itemnum);";
  if _n_=1 then code=_code;
  else code=strip(code)||strip(_code);
  if last then do;
  call execute('data wanted;set test;');
  call execute(code);
  call execute('run;');
  end;
run;
这是我根据您的命令修改的,麻烦您帮忙看一下哪里有问题。
总data有2000多条观测。

使用道具

8
wwang111 发表于 2016-7-22 22:08:36 |只看作者 |坛友微信交流群
length _code $100 code $1000; 定义长度的时候,字符型变量要加 $

如果长度1000不够用,可以设置的更长一点,字符型变量的长度最长可以到32767

使用道具

9
susanzhu 发表于 2016-7-22 23:27:45 |只看作者 |坛友微信交流群
wwang111 发表于 2016-7-22 22:08
length _code $100 code $1000; 定义长度的时候,字符型变量要加 $

如果长度1000不够用,可以设置的更长 ...
好的 多谢您了

使用道具

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

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

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

GMT+8, 2024-4-25 07:53