楼主: cindy七七
14544 30

SAS里如何取每个变量前几个数据 [推广有奖]

21
画纱无形 发表于 2013-7-10 17:05:54 |只看作者 |坛友微信交流群
SAS学的不好,方法比较笨

使用道具

22
jolterheadmmtt 发表于 2013-7-29 17:18:53 |只看作者 |坛友微信交流群
data test;
  input a: $1.  con:$2.;
   datalines;
   a    a1
   a    a2
   a    a3
   b    b1
   b    b2
   b    b3
   b    b4
   b    b5
   c    c1
   c    c2
   c    c3
   c    c4
   ;
run;

proc sort data=test out=test1;
  by a con;
run;

data test2;
  set test1;
  by a;
  if first.a or lag(first.a) or lag2(first.a);
  keep a con;
run;

使用道具

23
fossilweng 发表于 2013-7-30 02:48:18 |只看作者 |坛友微信交流群
学习了,
pobel,
thanks

使用道具

24
echo0 发表于 2015-6-13 11:02:08 |只看作者 |坛友微信交流群
这个办法好像只使用取值个数不多的情况,我想请教下如果是前100个甚至更多的值有什么办法吗?

使用道具

25
littlepig818 发表于 2015-6-14 20:18:19 |只看作者 |坛友微信交流群
echo0 发表于 2015-6-13 11:02
这个办法好像只使用取值个数不多的情况,我想请教下如果是前100个甚至更多的值有什么办法吗?
data a;
    input ID $ N $;
        cards;
a    a1
a    a2
a    a3
b    b1
b    b2
b    b3
b    b4
b    b5
c    c1
c    c2
c    c3
c    c4
;

data b;
   set a;
   by id;
   if first.id or lag(first.id) or lag2(first.id);
run;



以上是取前120个的。

使用道具

26
dengwei715 发表于 2015-6-15 12:01:45 |只看作者 |坛友微信交流群
data a;
    input ID :$1. N :$2.;
        cards;
a    a1
a    a2
a    a3
b    b1
b    b2
b    b3
b    b4
b    b5
c    c1
c    c2
c    c3
c    c4
;

data b;
   set a;
   by id;
   if first.id  then i=0;
   i+1;
   if i>=120 then delete;
   drop i;
run;

使用道具

27
letsgoaway 发表于 2015-6-15 16:52:49 |只看作者 |坛友微信交流群
加一列行号row=1,2,3,4,。。。。
然后用proc sql,

proc sql;
select * from mice group by id having row<min(row)+3;/*表名为mice,包含行号变量row*/
quit;

使用道具

28
三号铜豌豆 发表于 2016-4-10 13:32:36 |只看作者 |坛友微信交流群
if _n_<3  then 就代表取前三个观测值

使用道具

29
guanglei 发表于 2016-4-10 21:59:36 |只看作者 |坛友微信交流群
  1. data x;
  2. input id $ N $;
  3. cards;
  4.         a    a1
  5.    a    a2
  6.    a    a3
  7.    b    b1
  8.    b    b2
  9.    b    b3
  10.    b    b4
  11.    b    b5
  12.    c    c1
  13.    c    c2
  14.    c    c3
  15.    c    c4
  16.    ;
  17. run;

  18. proc sort data = x; by id; run;

  19. data result (keep = i N);
  20. set x; by id;
  21. IF first.id then DO ;i=0;end ;
  22. IF i<3 then DO; output;i+1;end;
  23. run;
  24.         
复制代码

使用道具

30
紫月170 发表于 2017-12-8 10:47:23 |只看作者 |坛友微信交流群
pobel 发表于 2013-7-10 16:08
data a;
    input ID :$     N :$;
        cards;
你好,请教一下如果要循环取每组的三个数怎么办呀?比如取前1-3个,然后取前2-4个,然后3-5.。。

使用道具

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

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

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

GMT+8, 2024-4-24 15:13