2013-07-30 53 views
1

我在SAS中創建以下表格尋求幫助。使用轉置的SAS多標誌

我有這個表:

Policy no Policy type ID_Payor1 ID_Payor2 ID_Insured1 ID_Insured2 ID_Owner1 ID_Owner2 
123 P1 A - B - A - 
124 P2 B - - - - - 
124 P1 A - C - C - 

我尋找創造是這樣的,將鞏固policytypes的數字,每個ID有:

ID Countflag_P1_Payor Countflag_P1_Owner Countflag_P1_Insured Countflag_P2_Payor Countflag_P2_Owner Countflag_P2_Insured  
A 2 1 0 0 0 0 
B 0 0 1 1 0 0 
C 0 1 1 0 0 0 

真的會appreacite你的幫助。 。

謝謝

+0

到目前爲止您嘗試過什麼?也許首先將你的數據集轉換爲垂直(每行一個ID_x,每個當前行6行)。 – Joe

+0

對此有何好運? –

回答

0

我想你想要一個proc f要求或總結這裏開始積累計數。爲A B C創建一個格式並在proc彙總中使用preloadfmt選項似乎是要走的路。

您希望創建格式並執行proc彙總的原因是因爲您需要生成一個數據集,該數據集針對每個policy_type計算A,B和C - 即使組合不存在。就像您的示例數據中沒有用於ID_payor1的C,但您仍然希望生成在該列中顯示C爲0的計數的行。

data table1; 
input Policy_no 
     Policy_type $ ID_Payor1 $ ID_Payor2 $ 
     ID_Insured1 $ ID_Insured2 $ ID_Owner1 $ ID_Owner2 $; 
datalines; 
123 P1 A . B . A . 
124 P2 B . . . . . 
124 P1 A . C . C . 
; 


proc format; 
    value $abc 
      'A'='A' 
      'B'='B' 
      'C'='C' 
      '.'='-' 
    ; 
run; 

proc summary data=table1 completetypes nway; 
    class ID_Payor1 ID_Payor2 
      ID_Insured1 ID_Insured2 ID_Owner1 
      ID_Owner2 /preloadfmt missing; 
    class policy_type; 
    types policy_type * (ID_Payor1 ID_Payor2 ID_Insured1 
         ID_Insured2 ID_Owner1 ID_Owner2); 
    output out=sumt1; 
    format ID_Payor1 ID_Payor2 ID_Insured1 ID_Insured2 ID_Owner1 ID_Owner2 $abc.; 
run; 


proc print data=sumt1; 

此時你sumt1數據集具有對每個列A B和C一頻率計數(和缺失爲 - )由每個變量與P1和P2。這幾乎不是你想要的,但現在已準備好轉換。這個數據集太大而不能在這裏打印 - 它很長而不是很寬,並且在列中有很多缺失值。但請檢查當時的proc打印結果,看看你得到了什麼。

對於轉置多個列,我們需要在每列上運行一次proc轉置,然後合併結果。宏觀似乎要走到這裏。

在轉置之前,我還將大數據集進行了子集化,因此我們只有包含我們轉置的列的數據的行。

%global freqtables; 

%MACRO transall(mCol); 

    data tmp_col; 
    set sumt1; 
    if &mCol. in ('A','B','C'); 

    proc transpose data=tmp_col 
        out=outTrans_&mCol.(rename= &mCol.=IDabc) prefix=&mCol._; 
     by &mCol.; 
     id policy_type; 
     var _FREQ_; 
    run; 

    %let freqtables = &freqtables outTrans_&mCol.(drop= _NAME_); 
    %* using freqtables macro variable to autogenerate a list; 
    %* of tables for merging later; 
%MEND transall; 

%transall(ID_Payor1); 
    %transall(ID_Payor2); 
    %transall(ID_Insured1); 
    %transall(ID_Insured2); 
    %transall(ID_Owner1); 
    %transall(ID_Owner2); 

*創建另一個宏循環通過變量的獎勵積分; *而不是上述 - 特別是如果你有更多的專欄;

data combined_counts; 
     merge &freqtables; 
     by IDabc; 
     run; 
proc print data=combined_counts; 

在這一點上,你應該有一個像你正在尋找的表。