关系数据库是否适合SAS处理?

时间:2013-11-01 03:05:42

标签: sql relational-database sas data-processing

目前,我有一个程序可以处理SAS中的原始数据,运行如下所示的查询:

/*this code joins the details onto the spine, selecting the details 
 that have the lowest value2 that is greater than value, or if there are none
 of those, then the highest value2*/
/*our raw data*/
data spine; 
    input id value; 
    datalines;
    1 5
    2 10
    3 6
;
run;

data details;
    input id value2 detail $; 
    datalines;
    1 6 foo
    1 8 bar
    1 4 foobar
    2 8 foofoo
    2 4 barbar
    3 6 barfoo
    3 2 foobarfoo
;
run;

/*cartesian join details onto spine, split into highs and lows*/    
proc sort data = spine;
by id; 
run; 

proc sort data= details;
    by id; 
run;

data lows highs;
    join spine details;
    by id;
    if value2 ge value then output highs;
    else output lows;
run;

/*grab just the first/last of each set*/
proc sort data =lows;
by id value2;
run;

proc sort data = highs;
by id value2; 
run;

data lows_lasts;
    set lows;
    by id;
    if last.id;
run;

data highs_firsts;
    set highs;
    by id;
    if first.id;
run;

/*join the high value where you can*/
data join_highs;
    merge spine(in = a)
    highs_firsts ;
    by id;
    if a;
run;

/*split into missing and not missng*/
data not_missing still_missing;
    set join_highs;
    if missing(value2) then output still_missing;
    else output not_missing; 
run;

/*if it doesn't already have a detail, then attach a low*/
data join_lows;
    merge still_missing(in = a)
    lows_lasts ;
    by id; 
    if a;
run;

/*append the record that had a high joined, and the record that had a low joined, together*/ 
data results;
    set not_missing join_lows;
run; 

你得到了照片。有许多这类数据处理语句,每周在新记录上运行。

还进行数据转换(例如清理/解析地址)。

现在 - 这种处理可以使用SQL完成。

问题是 - 这是否适当使用关系数据库?或者数据库是否应仅用于数据存储和检索?

考虑到我们正在谈论的行数高达1000万行。

2 个答案:

答案 0 :(得分:1)

SAS专为您提到的操作而设计!您绝对可以在RDBMS中完成大部分这些处理,但与SAS功能相比,典型RDBMS的分析功能有限。 SAS中也有一些优雅的结构,例如SAS First和Last处理的SQL替代方案更加繁琐。这些代码可以每周轻松地组织和安排为流程。

主要问题是你为什么要使用RDBMS,它有什么好处? 我想到了两个潜在的优势:

  • 多用户使用:RDBMS的内置锁定和事务管理允许多个数据库用户同时访问数据库。我认为情况并非如此。
  • 记录负载:如果有数十亿条记录,即使打开COMPRESS选项,SAS数据集的大小也很难处理(速度也可能是一个问题)。在这种情况下,将数据存储在RDBMS中并通过SAS / Access接口访问它是一种很好的做法。我认为情况并非如此。

如果您使用SAS解决方案中的RDBMS,请注意您可能必须重写代码的某些部分,因为如果使用libname方法运行某些SAS函数,则某些SAS函数不能与RDBMS-s一起使用。 FedSQL的使用(SAS 9.4中引入的SAS SQL:1999核心标准的SAS实现)正在解决这个问题。

另请注意,在某些特殊情况下,您可以使用SAS different results来使用RDBMS。

答案 1 :(得分:0)

正如您所指出的,这种类型的处理可以在RDBMS中完成,但不仅仅是关于任何RDBMS。您需要一个符合最新ANSI SQL标准的RDMBS,或至少支持具有WINDOW功能的PARTITION函数。例如:PostgreSQL,Teradata,SQL Server 2012等...注意:MySQL缺失,因为它不支持WINDOW函数。

相关问题