database - SAS:按照几个过程生成输出数据库

标签 database sas

我还是 SAS 的新人,我想知道如何执行以下操作:

假设我有一个包含以下信息的数据库:

Time_during_the day    date    prices   volume_traded
930am                  sep02    42            300
10am                   sep02    41            200
..4pm                  sep02    40            200
930am                  sep03    40            500
10am                   sep03    41            100
..4pm                  sep03    40            350
.....

我想要的是取每日总交易量的平均值,并将这个数字除以 50(总是)。所以说平均每日交易量/50 = V;我想要的是在大小 V 的每个间隔记录价格/时间/日期。现在,假设 V=500,我首先在数据库中记录第一个价格、时间和日期,然后记录相同的信息 500稍后进行批量交易。有可能有一天,交易量为 300,其中一半将覆盖 v=500,另外 150 将用于填充接下来的区间。

如何在一个数据库中获取这些信息? 谢谢!

最佳答案

假设您的输入数据集名为tick_data,并且它按 date 排序。和time_during_the_day 。然后这就是我得到的:

%LET n = 50;

/* Calculate V - the breakpoint size */
PROC SUMMARY DATA=tick_data;
    BY date;

    OUTPUT OUT = temp_1 
           SUM (volume_traded)= volume_traded_agg;
RUN;
DATA temp_2 ;
    SET temp_1;
    V = volume_traded_agg / &n;
RUN;

/* Merge it into original dataset so that it is available */
DATA temp_3;
    MERGE tick_data temp_2;
    BY date;
RUN;

/* Final walk through tick data to output at breakpoints */
DATA results 
    /* Comment out the KEEP to see what is happening under the hood */
    (KEEP=date time_during_the_day price volume_traded)
;
    SET temp_3;

    /* The IF FIRST will not work without the BY below */
    BY date;

    /* Stateful counters */
    RETAIN 
            volume_cumulative
            breakpoint_next
            breakpoint_counter
    ;

    /* Reset stateful counters at the beginning of each day */
    IF (FIRST.date) THEN DO;
            volume_cumulative   = 0;
            breakpoint_next     = V;
            breakpoint_counter  = 0;
    END;

    /* Breakpoint test */
    volume_cumulative = volume_cumulative + volume_traded;
    IF (breakpoint_counter <= &n  AND volume_cumulative >= breakpoint_next) THEN DO;
        OUTPUT;
        breakpoint_next = breakpoint_next + V;
        breakpoint_counter = breakpoint_counter + 1;
    END;
RUN;

future 要记住的关键 SAS 语言功能是使用 BY , FIRST ,和RETAIN一起。这使得能够对像这样的数据进行有状态的遍历。有条件OUTPUT这里也有数字。

请注意,每当您使用BY <var>时,数据集必须按包含 <var> 的键进行排序。以tick_data为例和所有中间临时表,它是。

附加:替代方案 V

为了使 V 等于(平均每日总交易量/n),请将上面的匹配代码块替换为以下代码块:

. . . . . .
/* Calculate V - the breakpoint size */
PROC SUMMARY DATA=tick_data;
    BY date;

    OUTPUT OUT = temp_1 
           SUM (volume_traded)= volume_traded_agg;
RUN;
PROC SUMMARY DATA = temp_1
    OUTPUT OUT = temp_1a
           MEAN (volume_traded_agg) =;
RUN;
DATA temp_2 ;
    SET temp_1a;
    V = volume_traded_agg / &n;
RUN;

/* Merge it into original dataset so that it is available */
DATA temp_3 . . . . . .
 . . . . . . 

基本上你只需插入第二个 PROC SUMMARY取总和的平均值。注意没有BY声明,因为我们是对整个集合进行平均,而不是按任何分组或桶进行平均。另请注意MEAN (...) = = 后没有名字。这将使输出变量与输入变量具有相同的名称。

关于database - SAS:按照几个过程生成输出数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11011439/

相关文章:

sql-server - 如何获取所有存储过程执行时间,杯子时间等

c++ - 什么时候值得使用数据库?

java - 如何使用java-使用多线程或循环在Oracle SQL中多次更新表

sas - SAS 中 TRANWRD 的问题

mysql - 如何获取Mysql中特定类别的最后一项

mysql - 仅当列值为今天的日期时才需要触发触发器

sas - 识别给定边的连通图

r - R 中数据帧的 [1], [1,], [,1], [[1]] 有什么区别?

excel - 使用 SAS 导出到 Excel

sas - 导出/导入 SAS Enhanced Editor 配色方案