楼主: jzl543098871
2681 18

[问答] 求助,多次整除并重新编号 [推广有奖]

11
jg.sas 发表于 2020-7-13 15:49:46
jzl543098871 发表于 2020-7-13 15:35
提示这个
MPRINT(LASTONE):   ? ? ? ? ? ? ? ? run;
NOTE: 由调用宏“LASTONE”生成行。
我这边程序运行没有问题

12
jzl543098871 学生认证  发表于 2020-7-13 16:08:19
jg.sas 发表于 2020-7-13 15:49
我这边程序运行没有问题
跑出来咋是33,我寻思应该是32啊,是我理解不对吗

13
jzl543098871 学生认证  发表于 2020-7-13 16:21:49
jg.sas 发表于 2020-7-13 15:49
我这边程序运行没有问题
还有,请教下,假如单数出列改为双数出列,那么除了mod(_n_,2)=0还有其他地方要改吗?

%do %until(&obsno=2 or &obsno=3);这句是直到变量的值为2或3,循环停止对吗?

n是观测值数量吗?怎么就出来了啊,前面句子也没提,就直接用了

14
jg.sas 发表于 2020-7-13 16:26:25
jzl543098871 发表于 2020-7-13 16:08
跑出来咋是33,我寻思应该是32啊,是我理解不对吗
mod(_n_,2)=0改为0应该就可以了

15
jg.sas 发表于 2020-7-13 16:27:27
jzl543098871 发表于 2020-7-13 16:21
还有,请教下,假如单数出列改为双数出列,那么除了mod(_n_,2)=0还有其他地方要改吗?

%do %until( ...
判断最后的数据集个数是不是最后一轮了,如果是最后一轮就把那个数选出来

16
jg.sas 发表于 2020-7-13 16:28:14
jzl543098871 发表于 2020-7-13 16:21
还有,请教下,假如单数出列改为双数出列,那么除了mod(_n_,2)=0还有其他地方要改吗?

%do %until( ...
_n_是SAS内置变量,就是obs序号

17
jzl543098871 学生认证  发表于 2020-7-13 16:42:15
jg.sas 发表于 2020-7-13 16:27
判断最后的数据集个数是不是最后一轮了,如果是最后一轮就把那个数选出来
好的,谢谢指导,我寻思应该是修改最后if _N_=1,得出来的倒是1

不过我还没吃透,在研究下

18
jg.sas 发表于 2020-7-13 17:01:08
  1. MPRINT(LASTONE):   data test;
  2. MPRINT(LASTONE):   do aa= 1 to 50;
  3. MPRINT(LASTONE):   output;
  4. MPRINT(LASTONE):   end;
  5. MPRINT(LASTONE):   run;

  6. NOTE: The data set WORK.TEST has 50 observations and 1 variables.
  7. NOTE: DATA statement used (Total process time):
  8.       real time           0.00 seconds
  9.       cpu time            0.00 seconds
  10.       

  11. MPRINT(LASTONE):   data test;
  12. MPRINT(LASTONE):   set test;
  13. MPRINT(LASTONE):   if mod(_n_,2)=0 then output;
  14. MPRINT(LASTONE):   run;

  15. NOTE: There were 50 observations read from the data set WORK.TEST.
  16. NOTE: The data set WORK.TEST has 25 observations and 1 variables.
  17. NOTE: DATA statement used (Total process time):
  18.       real time           0.00 seconds
  19.       cpu time            0.00 seconds
  20.       

  21. MPRINT(LASTONE):   data _null_;
  22. MPRINT(LASTONE):   set test nobs=count;
  23. MPRINT(LASTONE):   call symput('obsno',left(put(count,8.)));
  24. MPRINT(LASTONE):   stop;
  25. MPRINT(LASTONE):   run;
  26. NOTE: There were 1 observations read from the data set WORK.TEST.
  27. NOTE: DATA statement used (Total process time):
  28.       real time           0.00 seconds
  29.       cpu time            0.00 seconds
  30.       

  31. MPRINT(LASTONE):   data test;
  32. MPRINT(LASTONE):   set test;
  33. MPRINT(LASTONE):   if mod(_n_,2)=0 then output;
  34. MPRINT(LASTONE):   run;

  35. NOTE: There were 25 observations read from the data set WORK.TEST.
  36. NOTE: The data set WORK.TEST has 12 observations and 1 variables.
  37. NOTE: DATA statement used (Total process time):
  38.       real time           0.00 seconds
  39.       cpu time            0.01 seconds
  40.       

  41. MPRINT(LASTONE):   data _null_;
  42. MPRINT(LASTONE):   set test nobs=count;
  43. MPRINT(LASTONE):   call symput('obsno',left(put(count,8.)));
  44. MPRINT(LASTONE):   stop;
  45. MPRINT(LASTONE):   run;
  46. NOTE: There were 1 observations read from the data set WORK.TEST.
  47. NOTE: DATA statement used (Total process time):
  48.       real time           0.00 seconds
  49.       cpu time            0.00 seconds
  50.       
  51.                                                            The SAS System


  52. MPRINT(LASTONE):   data test;
  53. MPRINT(LASTONE):   set test;
  54. MPRINT(LASTONE):   if mod(_n_,2)=0 then output;
  55. MPRINT(LASTONE):   run;

  56. NOTE: There were 12 observations read from the data set WORK.TEST.
  57. NOTE: The data set WORK.TEST has 6 observations and 1 variables.
  58. NOTE: DATA statement used (Total process time):
  59.       real time           0.00 seconds
  60.       cpu time            0.00 seconds
  61.       

  62. MPRINT(LASTONE):   data _null_;
  63. MPRINT(LASTONE):   set test nobs=count;
  64. MPRINT(LASTONE):   call symput('obsno',left(put(count,8.)));
  65. MPRINT(LASTONE):   stop;
  66. MPRINT(LASTONE):   run;
  67. NOTE: There were 1 observations read from the data set WORK.TEST.
  68. NOTE: DATA statement used (Total process time):
  69.       real time           0.00 seconds
  70.       cpu time            0.00 seconds
  71.       

  72. MPRINT(LASTONE):   data test;
  73. MPRINT(LASTONE):   set test;
  74. MPRINT(LASTONE):   if mod(_n_,2)=0 then output;
  75. MPRINT(LASTONE):   run;

  76. NOTE: There were 6 observations read from the data set WORK.TEST.
  77. NOTE: The data set WORK.TEST has 3 observations and 1 variables.
  78. NOTE: DATA statement used (Total process time):
  79.       real time           0.00 seconds
  80.       cpu time            0.01 seconds
  81.       

  82. MPRINT(LASTONE):   data _null_;
  83. MPRINT(LASTONE):   set test nobs=count;
  84. MPRINT(LASTONE):   call symput('obsno',left(put(count,8.)));
  85. MPRINT(LASTONE):   stop;
  86. MPRINT(LASTONE):   run;
  87. NOTE: There were 1 observations read from the data set WORK.TEST.
  88. NOTE: DATA statement used (Total process time):
  89.       real time           0.00 seconds
  90.       cpu time            0.00 seconds
  91.       

  92. MPRINT(LASTONE):   data lastone;
  93. MPRINT(LASTONE):   set test;
  94. MPRINT(LASTONE):   if _n_=2;
  95. MPRINT(LASTONE):   run;

  96. NOTE: There were 3 observations read from the data set WORK.TEST.
  97. NOTE: The data set WORK.LASTONE has 1 observations and 1 variables.
  98. NOTE: DATA statement used (Total process time):
  99.       real time           0.00 seconds
  100.       cpu time            0.00 seconds
复制代码

19
jzl543098871 学生认证  发表于 2020-7-13 20:49:54
jg.sas 发表于 2020-7-13 17:01
我去跑了下,这种就是没写循环一步一步来的,请问前面加%的和没%有什么区别呢

这种不带宏的应该也可以写成循环吧?

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

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