zoukankan      html  css  js  c++  java
  • sas数据操作-by、merge

    By语句

    By语句用于规定分组变量,控制set,merge,update或modify语句

    官方说明:

    BY<DESCENDING> variable-1<...<DESCENDING>variable-n> <NOTSORTED> ; 

    specifies the variable that the procedure uses to form BY groups. You can specify more than one variable. By default, the procedure expects observations in the data set to be sorted in ascending order by all the variables that you specify or to be indexed appropriately.

    简单意思为:默认为升序,可以声明多个变量

    声明为降序:by descending var1 descending var2;每个变量前面都要写上descending.

    NOTSORTED:specifies that observations with the same BY value are grouped together, but are not necessarily sorted in alphabetical or numeric order. The observations can be grouped in another way (for example, in chronological order).

    声明所有by组的观测在一起,但是没有被排序,如果前面用到了descending,则会覆盖掉,使其无用

     

    data me;
      set sashelp.class;
    run;
    /*排序后me的内容会发生变化*/
    proc
    sort data=work.me; by sex age; run;
    /*排序后的变量才能使用by,没排序的变量如果放在排序变量前会出现错误,放在后面没事儿,这里是产生first和last观测值的步骤*/
    /*意义在于寻找by组内的第一个和最后一个观测值*/ data fst_lst;
    set me; by sex age ; fisrt_a = first.age; last_a = last.age; fisrt_n = first.sex; last_n = last.sex; run;

    /*复制到sas中会飘红,是格式问题,删除前面的空格即可*/
    data fst_data last_data;
      set me;
      by sex age;
      if first.sex then output fst_data;
      if last.sex then output lst_data;
    run;

    2:Merge语句

    Merge有四种形式,在连接时需要注意的几个问题是

    1:how each method treats duplicate values of common variables

    2:how each method treats missing values or nonmatched values of common variables

    注意的小问题

    如果有同名变量,那么后一个数据集的变量会覆盖前一个数据集中的同名变量

    The MERGE statement returns both matches and non-matches by default

    Merge的变量需要相同的type和name,但是不需要相同的length。

    如果没有相同的type,error和warning会写到log上,并且merge失败

    Merge前必须对by中的变量进行排序操作,或by变量有是索引列

    无论是哪种Merge,如果没有by,都会一轮转化为Unmatched Merge

    2.1:One to One Unmatched Merge

    SAS simply joins together the first observation from each data set, then the second observation from each data set, and so on

    就是一一对应的进行合并,按位置来,不按by中的变量来 

    sas内部的运作形式

    1:在描述文件中读取描述变量的信息,并和新创建的变量一起建立pdv,并赋为缺失值。

    2:按merge中数据集的顺序读取观测值,如果有同样的变量,则后面的变量覆盖掉前面的变量。当执行到run前面一句时,将pdv中的数据写入数据集,但是只会将新创建的变量赋值为缺失

    3:一直进行,知道所有观测值读入完毕

    Match Merge

    Match-merging combines observations from two or more SAS data sets into a single observation in a new data set according to the values of a common variable

    The number of observations in the new data set is the sum of the largest number of observations in each BY group in all data sets 

    sas内部的运作形式

    1:在描述文件中读取描述变量的信息,并和新创建的变量一起建立pdv,并赋为缺失值,同时建立first.variable 、last.variable变量

    2:sas比较每个数据集中的第一个by group来决定在新数据集中哪个应该先出现,sas将两个数据集中第一个出现的数据合并,并写入pdv。如果另一个数据集没有相应数据,则写入缺失值。

    3:sas将pdv中的数据写入新数据集,保留pdv的数据,将新创建的变量置为缺失。继续进行连接,直到第一个by group中的变量全部连接完毕。然后重复上述过程直到所有合并by group完毕。

    2.2:One to One Match Merge

    besides the BY variables,variables from the second data set will overwrite any variables having the same name in the first data set.  

    2.3:One-to-Many Match Merge

    只对对应的by组变量进行连接

    新数据集中的观测数量是所有by group中最大数量的和。

    对于一对多的merge 用一个和多个进行一一连接。

    2.3:Many-to-Many Match Merge

    一一对应完成后,剩下的跟同组的最后一个残留值对应,也是采取一一对应的策略,只要记牢一点,merge和set不对应的情况下情况pdv中数据集的原有值 

     2.4:对于数据不对应的情况

  • 相关阅读:
    mysql-5.7.15-winx64免安装版配置
    db2 表授权语句
    java 调用 .net webservice 示例
    打印内存高解决方案
    eclipse快捷键调试总结【转】
    DevExpress GridControl 自定义 summary 算法
    GEMR: Get the parent window for view
    弹出窗口
    WPF UI虚拟化
    WPF应用程序最小化到系统托盘
  • 原文地址:https://www.cnblogs.com/yican/p/3799318.html
Copyright © 2011-2022 走看看