2

私はまだ 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
.....

What I want is to take the average of the total daily volume and divide this number by 50 (always). So say avg.daily vol./50 = V; and what I want is to record the price/time/date at every interval of size V. Now, say that V=500, I start by recording the first price,time,and date in my database and then record the same info 500 volume trade later. It is possible that on one day that the traded volume is say 300 and half of it will cover the v=500, the other 150 will be use to fill up up the following interval.

How can I get this information in one database? Thank you!

4

1 に答える 1

3

入力データセットが 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;

将来のために留意すべき重要な SAS 言語機能はBY、 、FIRST、および をRETAIN一緒に使用することです。これにより、このようなデータのステートフル ウォークが可能になります。条件付きOUTPUTもここに表示されます。

BY <var>を使用するときはいつでも、データセットは を含むキーでソートする必要があることに注意してください<var>tick_dataおよびすべての中間一時テーブルの場合は、そうです。

追加: 代替 V

V を (1 日の平均総ボリューム / 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基本的には、合計の平均を取るために1 秒挿入するだけです。BYグループやバケットごとではなく、セット全体を平均しているため、ステートメントがないことに注意してください。MEAN (...) =の後に名前がないことにも注意して=ください。これにより、出力変数が入力変数と同じ名前になります。

于 2012-06-13T18:18:28.223 に答える