2013-09-25 54 views
1

我有給定客戶的開始和結束周,我需要爲他們訂閱的週數製作面板數據。我已經將數據操作成了一種簡單的轉換形式,但是當我轉置時,我沒有在開始和結束之間填入數週。希望有一個示例能夠闡明我的要求。周從0開始到61結束,所以61周以上的任何一週都是61,也是爲了簡單。如果他們訂閱仍然填充1,否則填充爲空。每週跟蹤客戶延期

ID Start_week End_week 

1  6   61 
2  0   46 
3  45   61 

什麼,我想

ID week0 week1 ... week6 ... week45 week46 week47 ... week61 
1  .  . ... 1 ... 1  1  1 ... 1 
2  1  1 ... 1 ... 1  1  0 ... 0 
3  0  0 ... 0 ... 1  1  1 ... 1 

回答

2

我看到了兩種方式來做到這一點。 我會去一個陣列的方法,因爲它可能會以最快的速度(單數據步驟),並沒有那麼複雜:

data RESULT (drop=start_week end_week); 
    set YOUR_DATA; 
    array week_array{62} week0-week61; 
    do week=0 to 61; 
     if week between start_week and end_week then week_array[week+1]=1; 
     else week_array[week+1]=0; 
    end; 
run; 

或者,你可以準備一個錶轉置,以通過創建一個記錄工作per id per id ::

data BEFORE_TRANSPOSE (drop=start_week end_week); 
    set YOUR_DATA; 
    do week=0 to 61; 
     if week between start_week and end_week then subscribed=1; 
     else subscribed=0; 
     output; 
    end; 
run; 
+0

proc transpose很容易,但這是一個有用的。謝謝 – user2448666

0

我沒有工作的語法,但對你的指導方針。

首先用CTE製作一個表格,或者物理上用數字0到61作爲行。然後將此表與訂閱的表連接起來。類似於

FROM sub 
INNER JOIN CTE 
ON CTE.week BETWEEN sub.Start_week AND sub.End_week 

現在,您將每週訂購一個客戶。轉置,你將有在幾周之間也填補了。

2

使用數組來創建變量。一個問題是SAS數組是1索引。

data input; 
input ID Start_week End_week; 
datalines; 
1  6   61 
2  0   46 
3  45   61 
; 

data output; 
array week[62] week0-week61; 
set input; 
do i=1 to 62; 
    if i > start_week and i<= (end_week+1) then 
     week[i] = 1; 
    else 
     week[i] = 0; 
end; 
drop i; 
run;