2017-04-27 25 views
0

我有以下結構的數據集:整蠱SAS轉任務

Application_ID Product_ID Product1_Category Product2_Category 
11111   a   m1     m3 
11111   b   n2     n4 
11111      k3     k5 
11111      t3     t7 

正如你所看到的,也有在此應用二PRODUCT_ID。每個產品都對應於他們的product_category。由於產品ID順序排列,第一個產品ID將對應於Product1_Category,依此類推。由於奇怪的數據結構,我不能使用proc轉置過程。這裏是結果應該是:

Application_ID Product_ID Product_Category 
11111   a   m1 
11111   a   n2 
11111   a   k3 
11111   a   t3 
11112   b   m3 
11113   b   n4 
11114   b   k5 
11115   b   t7 

我試圖使用proc轉置,但結果不是我想要的。

+0

你怎麼知道應該是什麼樣的產品ID,什麼是您的規則?根據SO規則,你嘗試了什麼,你需要展示你迄今爲止的嘗試以及你遇到的問題。 – Reeza

+0

@Reeza我在帖子中添加了更多信息。 – user5514406

+0

爲什麼您的Application_ID遞增地增加Product_ID b? – Longfish

回答

0

它看起來像前N行有CATEGORY_ID變量的值? 如果是這樣,那麼只需使用標準數組方法進行轉換,然後添加代碼以從前N行中合適的一行更新PRODUCT_ID。

data have; 
    input Application_ID $ Product_ID $ Product1_Category $ Product2_Category $; 
datalines; 
11111 a m1 m3 
11111 b n2 n4 
11111 . k3 k5 
11111 . t3 t7 
; 

%let varlist=product1_Category product2_category ; 
data want ; 
    set have ; 
    array p &varlist ; 
    do i=1 to dim(p); 
    if i <= nobs then set have(keep=product_id) point=i nobs=nobs; 
    Product_Category=p(i); 
    output; 
    end; 
    drop &varlist; 
run; 
proc print; run; 

enter image description here