楼主: jzc109
11667 6

SAS中如何有条件的取出自己想要的某个变量的某些值? [推广有奖]

  • 0关注
  • 0粉丝

本科生

72%

还不是VIP/贵宾

-

威望
0
论坛币
190 个
通用积分
0.0000
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
341 点
帖子
31
精华
0
在线时间
186 小时
注册时间
2007-10-1
最后登录
2025-8-10

楼主
jzc109 发表于 2016-1-21 16:14:37 |AI写论文
50论坛币
部分数据如下所示:
  1. data test;
  2. input id test1 test2 test3 test4 test5;
  3. cards;
  4. 1        .        20.9        2.8        1.06        7.97
  5. 2        .        1.69        .        5.4        4.67
  6. 3        1.5        .        .        .        .
  7. 4        10.1        0.39        8.88        10.12        5.2
  8. 5        5.2        1.7        6.2        18.7        5.9
  9. 6        1.6        1.2        .        .        .
  10. 7        3.32        1        21.3        5.5        6.5
  11. 8        2.4        1.8        .        .        .
  12. 9        1.76        .        4.42        .        .
  13. 10        .        1.06        .        3.81        11.7
  14. 11        9.11        .        0.7        4.03        2.83
  15. 12        6.05        1.03        .        .        7.15
  16. 13        4.14        .        .        1.99        8.89
  17. 14        .        1.98        .        2.34        3.28
  18. 15        .        .        .        0.8        3.81
  19. 16        2.36        0.78        3.47        7.65        4.84
  20. 17        1.38        15.77        24        12.8        7.4
  21. 18        17.19        .        0.99        6.11        7.98
  22. 19        1.35        0.8        0.51        3.4        2.61
  23. 20        1.85        19.74        9.39        5.13        4.49
  24. 21        0.87        10.92        4.52        3.75        2.56
  25. 22        2.56        1.86        0.91        5.23        2.42
  26. 23        2.44        0.13        1.61        9.37        2.4
  27. 24        .        1.29        3.5        25.5        5.88
  28. 25        1.07        32.9        16.83        14.06        3.67
  29. 26        2.84        2.92        2.21        0.95        3.93
  30. 27        1.46        9.14        7.56        4.25        3.18
  31. 28        4.41        .        .        .        1.75
  32. 29        1.35        29.17        13.79        12.68        3.58
  33. 30        1.89        21.36        11.21        5.54        2.77
  34. 31        1.05        .        11.9        5.83        1.46
  35. 32        0.84        0.18        0.77        6.71        3.35
  36. 33        5.57        9.57        1.49        7.11        6.28
  37. 34        1.58        4.56        18.58        6.24        6.82
  38. 35        1.71        16.48        .        8.67        6.31
  39. 36        15.66        2.6        1.84        3.09        4.77
  40. 37        2.09        1.51        1.27        15.15        4.48
  41. 38        3.32        14.6        5.77        1.52        3.79
  42. 39        6.52        0.57        2.09        9.53        3.57
  43. 40        1.9        15.2        2.4        2.2        3.24
  44. 41        .        27.91        12.85        0.45        6.21
  45. 42        .        .        1.01        2.59        4.3
  46. 43        0.81        1.32        3.51        13.14        3.87
  47. 44        4.54        .        .        .        0.8
  48. 45        5.72        .        .        .        0.56
  49. 46        .        5.15        .        0.69        2.17
  50. 47        2.56        .        .        0.4        .
  51. 48        3.79        .        .        .        0.51
  52. 49        3.84        .        3.23        0.7        2.24
  53. 50        7.5        0.3        22.3        7        4.5
  54. run;
复制代码
     问题是这样的。我想取出每个观测中小于2的值以及这个值之后离他最近的大于2的值,建立一个数据集。如果一条记录中有多个小于2的值,只要第一个小于2的变量(如第19个观测)。如果某个记录只有小于2的变量(如第三个观测)或者所有的变量都小于2(如第47个观测)则表这条记录为删失。
在试着用

  %do i=1 %to 5;
      %let j= %eval(&i+1);

的循环时只能实现部分功能,特来请教。


最佳答案

孤单的我们 查看完整内容

程序里加了条件:var1 var2有1个缺失,则删除观测。
关键词:Input cards test Data card 如图所示 如何

沙发
孤单的我们 发表于 2016-1-21 16:14:38
  1. data want;        
  2.         set test;
  3.         array test test1 test2 test3 test4 test5;
  4.         do i=1 to dim(test);
  5.                 if missing(var1) & test{i}<2 then var1=test{i};
  6.                 if ^missing(var1) then do;
  7.                 do j=i to dim(test);
  8.                         if missing(var2) & test{j}>2 then var2=test{j};
  9.                 end;
  10.                 end;
  11.         end;
  12.         if nmiss(var1,var2)=0;
  13.         drop i j;
  14. run;
复制代码


程序里加了条件:var1 var2有1个缺失,则删除观测。
已有 1 人评分论坛币 收起 理由
admin_kefu + 20 热心帮助其他会员

总评分: 论坛币 + 20   查看全部评分

藤椅
yingzi2003 发表于 2016-1-22 03:59:03
data test_new (drop=flag_2) ;
  set test ;
  array test test1-test5 ;
  do i = 1 to 5 ;
    if test[i] > 2 then flag_2 = 1 ;
        if test[i] < 2 and low_2 = . then low_2=test[i] ;
        if low_2 ne . and high_2 = . and test[i] > 2 then high_2 = test[i] ;
  end ;
  if flag_2 = . then delete ;
  drop i ;
run;
已有 1 人评分论坛币 收起 理由
admin_kefu + 20 热心帮助其他会员

总评分: 论坛币 + 20   查看全部评分

板凳
Tigflanker 发表于 2016-1-22 09:08:20
1. 你有点没说明白:
...或者所有的变量都小于2(如第47个观测)...
第47条观测是:47 2.56 . . 0.4 .

2. 我给你推荐一个函数,smallest,你把这个函数的用法看明白,你就知道咋做了。

报纸
teqel 发表于 2016-1-22 11:38:33
孤单的我们 发表于 2016-1-21 17:19
程序里加了条件:var1 var2有1个缺失,则删除观测。
do i=1 to dim(test) until var1; 这种句子能工作吗?

地板
kunkunred 发表于 2016-1-23 05:04:28
Here you go.

data test1(drop=i );
set test;
array test{*} test1-test5;
do i=1 to dim(test) ;
if ^missing(test{i}) & test{i}<2  then do;  keep=test{i} ; i=dim(test); end;
end;
if max(of test:)<2 then delete;
run;
已有 1 人评分论坛币 收起 理由
admin_kefu + 15 热心帮助其他会员

总评分: 论坛币 + 15   查看全部评分

7
孤单的我们 发表于 2016-1-23 15:28:41
teqel 发表于 2016-1-22 11:38
do i=1 to dim(test) until var1; 这种句子能工作吗?
没见过这个用法

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

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