Komplex transposing of a dataset

  • Follow


Hello,

i have a SAS-dataset like this:

KDNR    X1 X2 ..... X10     Y
------------------------------------
001        ....                ....        A
001        ....                .....        B
001     .....    ...................        C
002     ............................       B
003    ..............................      A
003   ..............................        B

For each customer, for the different values of Y, the figures X1-X10 .
Multiple row per customer.


I want to convert the dataset in a form like this

KDNR      A                             B                       C
             X1 ......... X10 X1 ......... X10    X1 ......... X10
--------------------------------------------------------------
001
.............................................................................
..
002                             .........................
003    ..................................................

One row for each customer. Different variables for X1-X10 for the different
values of Y.


How can i do this with SAS?

Thank you.

J�rg Neumann
Nueremberg, Germany


0
Reply neumann1 (3) 9/16/2004 5:19:37 PM

Jorg (Sorry, no umlaut on my keyboard if that's what they call it in
Danish)

Try the following simplified example. It appears to do what you wish.

Nat Wooding

data a;
input kdnr x1 x2 y $10.;

cards;
1  11  22   A
1  22  33   B
1  33  44   C
2  44  33   B
3  77  88   C
proc print;
proc transpose out=a;
  by kdnr y;
data trans1;
     set;
       newvar=compress(y||_name_);
       drop y _name_;
proc transpose out=trans2 (drop _:) ;
     by kdnr;
       id newvar;
       var col1;
proc print;run;




                      J�rg Neumann
                      <neumann@CONSORS.        To:       SAS-L@LISTSERV.UGA.EDU
                      DE>                      cc:
                      Sent by: "SAS(r)         Subject:  Komplex transposing of a dataset
                      Discussion"
                      <SAS-L@LISTSERV.U
                      GA.EDU>


                      09/16/04 01:19 PM
                      Please respond to
                      J�rg Neumann






Hello,

i have a SAS-dataset like this:

KDNR    X1 X2 ..... X10     Y
------------------------------------
001        ....                ....        A
001        ....                .....        B
001     .....    ...................        C
002     ............................       B
003    ..............................      A
003   ..............................        B

For each customer, for the different values of Y, the figures X1-X10 .
Multiple row per customer.


I want to convert the dataset in a form like this

KDNR      A                             B                       C
             X1 ......... X10 X1 ......... X10    X1 ......... X10
--------------------------------------------------------------
001
.............................................................................

..
002                             .........................
003    ..................................................

One row for each customer. Different variables for X1-X10 for the different
values of Y.


How can i do this with SAS?

Thank you.

J�rg Neumann
Nueremberg, Germany
0
Reply Nathaniel_Wooding (697) 9/16/2004 5:55:11 PM


On Thu, 16 Sep 2004 19:19:37 +0200, J�rg Neumann <neumann@CONSORS.DE> wrote:

>Hello,
>
>i have a SAS-dataset like this:
>
>KDNR    X1 X2 ..... X10     Y
>------------------------------------
>001        ....                ....        A
>001        ....                .....        B
>001     .....    ...................        C
>002     ............................       B
>003    ..............................      A
>003   ..............................        B
>
>For each customer, for the different values of Y, the figures X1-X10 .
>Multiple row per customer.
>
>
>I want to convert the dataset in a form like this
>
>KDNR      A                             B                       C
>             X1 ......... X10 X1 ......... X10    X1 ......... X10
>--------------------------------------------------------------
>001
>...........................................................................
..
>.
>002                             .........................
>003    ..................................................
>
>One row for each customer. Different variables for X1-X10 for the different
>values of Y.
>
>
>How can i do this with SAS?
>
>Thank you.
>
>J�rg Neumann
>Nueremberg, Germany

Try the following:

data xx;
input KDNR $ X1 X2 X3 Y $;
cards;
001 1 3 9 A
001 2 5 8 B
001 0 0 0 C
002 1 1 1 B
003 3 4 5 A
003 8 8 8 B
;

data xx;
 set xx;
 array xx x1-x3;
 do over xx;
 _name_=trim(y)||'_'||vname(xx);
 _val_=xx;
 output;
 end;
run;

proc sort;
by kdnr _name_;
run;

proc transpose data=xx out=yy (drop=_name_);
by kdnr;
var _val_;
id _name_;
run;

proc print;
run;

--------------------
KDNR    A_X1    A_X2    A_X3    B_X1    B_X2    B_X3    C_X1    C_X2

001       1       3       9       2       5       8       0       0
002       .       .       .       1       1       1       .       .
003       3       4       5       8       8       8       .       .


Kind regards,

Ya Huang
0
Reply yhuang (489) 9/16/2004 5:58:33 PM

Here's a one-pass solution. I'll illustrate with 2 instead of 10.

   data have;
   input KDNR $ X1 X2 Y $;
   cards;
   001 111 112 A
   001 121 122 B
   001 131 132 C
   002 221 222 B
   003 311 312 A
   003 321 322 B
   ;

   proc format; invalue $row 'A'=1 'B'=2 'C'=3; run;

   data want;
   array in(2) x1-x2;
   drop  y i j x1-x2;
   array out(3,2) A1-A2 B1-B2 C1-C2;
   do until (last.kdnr);
      set have;
      by kdnr;
      i = input(y,$row.);
      do j = 1 to 2;
         out(i,j) = in(j);
         end;
      end;
   run;

This assumes that you know the values of Y in advance.

On Thu, 16 Sep 2004 19:19:37 +0200, J�rg Neumann <neumann@CONSORS.DE> wrote:

>Hello,
>
>i have a SAS-dataset like this:
>
>KDNR    X1 X2 ..... X10     Y
>------------------------------------
>001        ....                ....        A
>001        ....                .....        B
>001     .....    ...................        C
>002     ............................       B
>003    ..............................      A
>003   ..............................        B
>
>For each customer, for the different values of Y, the figures X1-X10 .
>Multiple row per customer.
>
>
>I want to convert the dataset in a form like this
>
>KDNR      A                             B                       C
>             X1 ......... X10 X1 ......... X10    X1 ......... X10
>--------------------------------------------------------------
>001
>...........................................................................
..
>.
>002                             .........................
>003    ..................................................
>
>One row for each customer. Different variables for X1-X10 for the different
>values of Y.
>
>
>How can i do this with SAS?
>
>Thank you.
>
>J�rg Neumann
>Nueremberg, Germany
0
Reply Howard_Schreier (1506) 9/20/2004 7:55:58 PM

3 Replies
28 Views

(page loaded in 0.086 seconds)


Reply: