zoukankan      html  css  js  c++  java
  • sas数据操作set语句及其相关选项,widgets

    set语句:

    set语句有什么用?

    试想如果要给数据集增加一列(固定列或者计算列),增加新变量或者创建子集

    下面给出创建新列和增加固定列data步和sql过程的办法

    data me(keep=name newVariable total);
        set sashelp.class;
       if sex='男'; newVariable
    =.; total = height+weight; run; proc print noobs;run; proc sql; select name, '.' as newVariable, height+weight as total from sashelp.class
    where sex='男'; quit;

    Set statement

      Type: Executable

      Syntax

        SET<SAS-data-set(s)<(data-set-option(s))>><options>

      Without Arguments

        when you do not specify an argument, the SET statement reads an observation from the most recently created data set.

      Arguments

        SAS-data-set(s): specifies a one-level name, a two-level name, or one of the special SAS data set names.

        (data-set-options): specifies actions SAS is to take when it reads variables or observations into the program data vector for processing. 

          DROP=  KEEP=  RENAME=  (execution sequence: drop>keep>rename)

          FIRSTOBS=(first obs to be read)  

          OBS=(last obs to be read)   IN=   WHERE=

      Options

        END: creates and names a temporary variable that contains an end-of-file indicator. The variable, which is initialized to zero, is set to 1 when SET reads the last observation of the last data set listed. This variable is not added to any new data set.

        NOBS:creates and names a temporary variable whose value is usually the total number of observations in the input data set or data sets. If more than one data set is listed in the SET statement, NOBS= the total number of observations in the data sets that are listed. The number of observations includes those observations that are marked for deletion but are not yet deleted.

        POINT:specifies a temporary variable whose numeric value determines which observation is read. POINT= causes the SET statement to use random (direct) access to read a SAS data set.

      Details

        What Set Does?

          Each time the SET statement is executed, SAS reads one observation into the program data vector. SET reads all variables and all observations from the input data sets unless you tell SAS to do otherwise. A SET statement can contain multiple data sets; a DATA step can contain multiple SET statements

        

    按从前到后的顺序纵向堆叠数据集1-n。

    sas程序内部执行的过程如下:

    1:编译阶段

    SAS reads the descriptor information of each data set that is named in the SET statement and then creates a program data vector that contains all the variables from all data sets as well as variables created by the DATA step

    2:SAS reads the first observation from the first data set into the program data vector. It processes the first observation and executes other statements in the DATA step. It then writes the contents of the program data vector to the new data set.

    The SET statement does not reset the values in the program data vector to missing, except for variables whose value is calculated or assigned during the DATA step. Variables that are created by the DATA step are set to missing at the beginning of each iteration of the DATA step. Variables that are read from a data set are not.  (sas不会将pdv中原数据集的内容清空,新生成的变量除外)

    3:SAS continues to read one observation at a time from the first data set until it finds an end-of-file indicator. The values of the variables in the program data vector are then set to missing, and SAS begins reading observations from the second data set, and so on, until it reads all observations from all data sets

    对于带by的set data1-datan

    1:基于前面的描述增加 SAS creates the FIRST.variable and LAST.variable for each variable listed in the BY statement

    2:清空变量的方式有不同,The values of the variables in the program data vector are set to missing each time SAS starts to read a new data set and when the BY group changes。

    根据by组的改变来清空,当by组改变时会进行清空。

    然后根据by进行观测值的排序

    对于两个已经排好序的数据集,如果想要合并后依然排好序,有两种方法

    第一种:set data1 data2;然后再执行proc sort。

    第二种:set data1 data2;by variable;这种效率比第一种高,虽然不知道why...但是书上这么说的。我觉得可能是数据读取次数的问题吧,第二种只需要读一次,第一种要读两次

    set语句从一个或多个sas数据集中读取观测值并实现纵向合并,每一个set语句执行时,sas就会读一个观测到pdv中,一个data步可以有多个set语句,每个set语句可以跟多个sas数据集,多个set语句含有多个数据指针。

    set会将输入数据集中的所有观测值和变量读取,除非你中间执行其他步骤

    SET<SAS-data-set(s)<(data-set-options(s) )>><options>;

    (data-set-options) specifies actions SAS is to take when it reads variables or observations into the program data vector for processing.

    Tip:Data set options that apply to a data set list apply to all of the data sets in the list. Data set options specify actions that apply only to the SAS data set with which they appear. They let you perform the following operations:

    主要的功能是以下四天,并给出相关例子

    renaming variables   ex--> set sashelp.class(rename = (name = name_new));

    selecting only the first or last n observations for processing sashelp.class(where =(sex='M')); where和rename要用括号括起来

    dropping variables from processing or from the output data set sashelp.class(drop =name sex);sashelp.class(keep=name sex);

    specifying a password for a data set

    输出两个数据集

    data d1(keep = name) d2(keep = name sex);

       set sashelp.class(keep = name sex);

    run;

    IN=选项应用

    IN本身不是变量,所以不能通过赋值语句获得,IN=的最大作用是标识不同的数据集  

    data one;
        input x y$;
        cards;
        1 a
        2 b
        ;
    run;
    data two;
        input x z$;
        cards;
        3 c
        2 d
        ;
    run;
    
    data me;
        set one(in=ina)two(in=inb);
        if ina=1 then flag=1;else flag=0;
    run;

    res:

     

    data me;
     set sashelp.class(firstobs=3 obs=6);   /*读取第三到第六个变量*/
    run;

    *获取数据集中的总观测数;

    data me(keep = total);
     set sashelp.Slkwxl nobs=total_obs;   *if 0 then  set sashelp.Slkwxl nobs=total_obs;改进语句,因为sas是先编译再执行,所以可以选择不执行,只获取编译的信息就足够了
     total = total_obs;
     output;
     stop; *这里用stop是因为,我们只要象征性读取set中的第一条即可,输出total变量,然后终止程序;
    run;

    set的流程是这样的,先set第一个观测值,然后往下执行total=total_obs;然后继续执行,遇到stop则停止,否则在没遇到错误的情况下会返回data步开头继续set第二行观测值,所以,如果不屑stop语句,则会出来总数个相同的值为总数的变量

    1:程序编译时首先读nobs=选项,该选项在头文件中,nobs=total_obs将总观测数传给临时变量total_obs

    2:pdv读入数据集,并把所有变量放入pdv。

    。。。。省略

    POINT=选项 取指定的一条观测

    data me;
     n=3;
     set sashelp.class point=n;

    output; 

    do n=3,6,10;

    set sashelp.class point=n;

    output;*获取多个指定行的观测;

    end;

    set sashelp.class nobs = last point=last;
    output; *获取最后一行观测值;
     stop;
    run;

    point=n对应的是变量,不能直接赋值数字省略stop后会让程序进入死循环,不用stop语句sas无法判断该数据指针是否指向了最后一条观测,从而会陷入死循环。如果不用output,会得不到数据集,point和stop一般是连在一起使用

    _N_的使用

    data d1 d2;

    set sashelp.class;

     if _n_ le 10 then output d1;

    else output d2;

    run;

    set读取序列数据集合的一些注意事项:

        set goods1:;*读取所有以good1开头的文件,比如goods12 goods13;
        set sales1-sales4; set sales1 sales2 sales3 sales4;*这两条语句等价;
        /*
        set sales1-sales99; *合法;
        set sales001-sales99; *不合法,如果以0开头,那么后面的文件的数字要比前面的文件的数值的位数多,至少是相等;
        */
        set cost1-cost4 cost2: cost33-37; *可行;
    
        /* these two lines are the same */
        set sales1 - sales4;
        set 'sales1'n - 'sales4'n;
    
        /* blanks in these statements will cause errors */
        set sales 1 - sales 4;
        set 'sales 1'n - 'sales 4'n;
        /* trailing blanks in this statement will be ignored */
        set 'sales1   'n - 'sales4   'n;

    set data1 data2;

    其执行顺序为先读取data1,直至data1的最后一条语句后再读取data2,并将其纵向合并。

    双set语句

    set data1;set data2;

    data a;
        input x $ @@;
        cards;
        a1 a2 a3
        ;
    run;
    
    data b;
        input y $ @@;
        cards;
        b1 b2
        ;
    run;
    
    /*编译后内存出现两条数据指针分别指向a b,同时产生一个pdv*/
    /*读取数据集a的第一条观测进入pdv,数据集b的第一条观测进入pdv,然后输出,再返回data步开头,重复进行,当读入a的第三行时,b中的指针已经指向了文件尾部,所以跳出data步*/ data ab;
    set a;set b; run; data ba; set b;set a; run; data c; set a b; run;

    Widgets:

      1. Get the count of observations.

    %macro Get_Obs_Cnt(dsName);
    Data test;
        call symput('n_obs', last);
        if 0 then set &dsName nobs=last;
    run;
    %mend Get_Obs_Cnt;
    %put 'n_obs=' &n_obs;

      2. select random observations.

    %macro Generate_Random_Obs(inData, outData, num);
        data &outData;
            rand_num = ceil(totalObs*ranui(totalObs));
            do i=1 to &num;
                set &inData nobs=totalObs point=rand_num;
                    if(_error_) then abort;
                output;
            end;
            stop;
        run;
    %mend Generate_Random_Obs;
  • 相关阅读:
    在Visual Studio中使用层关系图描述系统架构、技术栈
    在Visual Studio中使用活动图描述业务流程
    在Visual Studio中使用类图描述领域模型
    在Visual Studio中使用用例图描述参与者与用例的关系
    在Visual Studio中使用用例图描述系统与参与者间的关系
    并行编程中的取消任务、共享状态,等等
    等待所有或任意异步任务完成,以及异步任务完成时的处理方案
    使用IProgress实现异步编程的进程通知
    Task.FromResult应用场景举例
    Task.Delay方法的2个应用实例,单元测试等待,限时限次下载远程资源
  • 原文地址:https://www.cnblogs.com/yican/p/3793856.html
Copyright © 2011-2022 走看看