楼主: fyfzhdsfdx
4003 17

[原创博文] SAS中观测数的疑问,及程序改进 [推广有奖]

11
pobel 在职认证  发表于 2012-10-11 15:27:32
fyfzhdsfdx 发表于 2012-10-11 15:09
谢谢,看来我要好好的学习一下数组和宏。不过这个程序会丢掉class数据集的第一个观测值,我把内循环改成do ...
确实上个程序会丢掉第一个观测。试试这个:
data name;
   set sashelp.class end=last;
   keep name;
   if last then call symputx("nobs",_n_);
run;

proc transpose data=name out=name1;
   var name;
run;

data name2;
    array name(6) $;
        array col(&nobs) $ col1-col&nobs;
        set name1;
    do i=1 to &nobs-5;
            do j=0 to 5;
                   name(j+1)=col(i+j);
                end;
                   output;
        end;
        keep name:;
        stop;
run;
和谐拯救危机

12
fyfzhdsfdx 发表于 2012-10-11 19:01:29
pobel 发表于 2012-10-11 15:27
确实上个程序会丢掉第一个观测。试试这个:
data name;
   set sashelp.class end=last;
这个事可以的,我试过,不过有警告,之前的回复说了,求改进,嘿嘿。。。谢谢。还有为什么要对name数据集转置为name1数据集,有什么作用?数据集name1在这里什么用?疑惑中。

13
fyfzhdsfdx 发表于 2012-10-11 19:30:17
bobguy 发表于 2012-10-10 06:42
Using the point access it would be simple in logic and no macro is necessary.

data out1_d;
还想问个问题,关于stop语句的。我只知道stop语句可以停止当前的data步,不影响后面的data步或者proc步的执行,但是在这里的作用是什么?循环从i=1到184不就自动结束了吗?谢谢!

14
fyfzhdsfdx 发表于 2012-10-11 19:42:01
pobel 发表于 2012-10-11 15:27
确实上个程序会丢掉第一个观测。试试这个:
data name;
   set sashelp.class end=last;
刚才的回复有问题,不好意思啊,我知道name1数据集的作用。现在的程序可以,我试过,结果正确,不过有警告,之前的回复说了,求改进,嘿嘿。。。谢谢。但是 keep name:;这句我不怎么理解,疑惑中。我还不知道keep语句的这种用法,求详细解析,谢谢!

15
bobguy 发表于 2012-10-12 07:09:13
fyfzhdsfdx 发表于 2012-10-11 19:30
还想问个问题,关于stop语句的。我只知道stop语句可以停止当前的data步,不影响后面的data步或者proc步的 ...
A data step stops a internal loop when end-of-file is reached or a stop statement is executed.

The end-of-file  is ignored when point access is used in a data step. So a explicit stop statement is needed to stop its looping.

16
pobel 在职认证  发表于 2012-10-12 07:21:55
fyfzhdsfdx 发表于 2012-10-11 19:42
刚才的回复有问题,不好意思啊,我知道name1数据集的作用。现在的程序可以,我试过,结果正确,不过有警告 ...
我这边运行没有警告:

1    data name;
2       set sashelp.class end=last;
3       keep name;
4       if last then call symputx("nobs",_n_);
5    run;

NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.NAME has 19 observations and 1 variables.
NOTE: DATA statement used (Total process time):
      real time           0.29 seconds
      cpu time            0.01 seconds


6
7    proc transpose data=name out=name1;
8       var name;
9    run;

NOTE: There were 19 observations read from the data set WORK.NAME.
NOTE: The data set WORK.NAME1 has 1 observations and 20 variables.
NOTE: PROCEDURE TRANSPOSE used (Total process time):
      real time           0.04 seconds
      cpu time            0.03 seconds


10
11   data name2;
12       array name(6) $;
13           array col(&nobs) $ col1-col&nobs;
14           set name1;
15       do i=1 to &nobs-5;
16               do j=0 to 5;
17                      name(j+1)=col(i+j);
18                   end;
19                      output;
20           end;
21           keep name:;
22           stop;
23   run;

NOTE: There were 1 observations read from the data set WORK.NAME1.
NOTE: The data set WORK.NAME2 has 14 observations and 6 variables.
NOTE: DATA statement used (Total process time):
      real time           0.10 seconds
      cpu time            0.04 seconds

Keep name:; 这句表示keep所有以“name”开头的变量。相当于:
keep name1 name2 name3 name4 name5 name6; /*将所有变量一一列出*/
keep name1-name6;  /*表示keep name1, name2,.. 一直到 name6*/
keep name1--name6; /*按照变量在数据集中的位置keep name1 和name6之间的所有变量,对名字没有要求*/
和谐拯救危机

17
fyfzhdsfdx 发表于 2012-10-15 08:38:00
pobel 发表于 2012-10-12 07:21
我这边运行没有警告:

1    data name;
很详细,谢谢!

18
fyfzhdsfdx 发表于 2012-10-15 09:13:15
bobguy 发表于 2012-10-12 07:09
A data step stops a internal loop when end-of-file is reached or a stop statement is executed.

...
I  got it.Thank you very much。

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

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