重组数据表/使用空值输入新行

时间:2016-07-14 16:35:20

标签: sas

有点棘手的问题。我需要系统地分组,排序和创建新的数据行的方法。有三个4级分层(c1,c2,c3,c4)和一个"分数"每次迭代分层的价值。这有点难以表达,所以我会给你HAVE和WANT表

以下是数据表格,便于操作。

data HAVE;
   input CLASS $ FUR_COLOR $ FAMILY $ SPECIES $ NO_IN_CAPTIVITY;
   datalines;
MAMMAL BLACK WHALE KILLER_WHALE 2 
MAMMAL BLACK FELINE PUMA 3
MAMMAL BLACK APE CHIMP 5
MAMMAL BLACK APE GORILLA 3
MAMMAL BLACK APE BONOBO 3
MAMMAL RED APE ORANGUTAN 2
;
RUN;

data WANT;
   input C1 $ C2 $ C3 $ C4;
   datalines;
MAMMAL . BLACK                .
MAMMAL WHALE KILLER_WHALE 2
MAMMAL . BLACK .
MAMMAL FELINE PUMA 3
MAMMAL . BLACK .
MAMMAL APE CHIMP 5
MAMMAL APE GORILLA 3
MAMMAL APE BONOBO 3
MAMMAL . RED .
MAMMAL APE ORANGUTAN 2
;
RUN;

2 个答案:

答案 0 :(得分:1)

你可以这样使用PROC SQLUNION ALL

PROC SQL;

CREATE TABLE want AS

    SELECT
        C1, C2, C4, SCORE
    FROM
        have

    UNION ALL

    SELECT
        C1, NULL, C3, NULL
        /* If your version does not support NULL, use '' */
    FROM
        have;

QUIT;

输出:

enter image description here

答案 1 :(得分:1)

这个怎么样?

data want;
  set have(rename=(C2=C2_in C3=C3_in C4=C4_in SCORE=SCORE_in));
  by C2_in C3_in;
  format C1-C3 $1. SCORE 3.;

  if first.C2_in or first.C3_in then do;
    C3 = C3_in;
    output;
  end;

  C2 = C2_in;
  C3 = C4_in;
  SCORE = SCORE_in;
  output;

  drop C2_in C3_in C3_in C4_in SCORE_in;
run;

Resulting dataset