0

次のようなデータセットがあります。

IDnum    State    Product    Consumption
123      MI       A          30
123      MI       B          20
123      MI       C          45
456      NJ       A          15
456      NJ       D          10
789      MI       B          60
...      ...      ...        ...

そして、IDnumごとに1つの行があり、異なる製品ごとに新しいダミー変数がある新しいデータセットを作成したいと思います(私の実際のデータセットでは、1000近くの製品があります)。これらの行の何かのように見えます

IDnum   State   Prod.A   Cons.A   Prod.B   Cons.B   Prod.C   Cons.C   Prod.D   Cons.D
123     MI      yes      30       yes      20       yes      45       no       -
456     NJ      yes      15       no       -        no       -        yes      10
789     MI      no       -        yes      60       no       -        no       -
...     ...     ...      ...      ...      ...      ...      ...      ...      ...

「状態」などの一部の変数は同じ IDnum 内で変化しませんが、元の銀行の各行は 1 つの購入に相当するため、同じ IDnum の「製品」変数と「消費」変数が変化します。新しいデータセットで、各コスチューム利用者のすべての消費習慣を 1 行で表示したいのですが、これまでのところ失敗しています。

どんな助けでも大歓迎です。

4

1 に答える 1

1

yes/no 変数がなければ、とても簡単です。

data input;
length State $2 Product $1;
input IDnum    State    Product    Consumption;
cards;
123      MI       A          30
123      MI       B          20
123      MI       C          45
456      NJ       A          15
456      NJ       D          10
789      MI       B          60
;
run;

proc transpose data=input out=output(drop=_NAME_) prefix=Cons_;
  var Consumption;
  id Product;
  by IDnum State;
run;

yes/no フィールドの追加:

    proc sql;/* from column names or alternatively 
                create it from source data directly if not taking too long */
    create table work.products as
        select scan(name, 2, '_') as product length=1
        from dictionary.columns 
                    where libname='WORK' and memname='OUTPUT' 
                      and  upcase(name) like 'CONS_%';
    quit;

    filename vars temp;/* write a temp file containing variable definitions
                       in desired order */
    data _null_;
        set  work.products end=last;
        file vars;
                    length str $40;
        if _N_ = 1 then put 'LENGTH ';
        str = catt('Prod_', product, ' $3');
        put str;
        str = catt('Cons_', product, ' 8');
        put str;
        if last then put ';';
    run;

    options source2;
    data output2;
        length IdNum 8 State $2;
        %include vars;
        set output;
        array prod{*} Prod_:;
        array cons{*} Cons_:;
        drop i;
        do i=1 to dim(prod);
            if coalesce(cons(i), 0) ne 0 then prod(i)='yes';
            else prod(i)='no';
        end;
    run;
于 2012-07-15T10:42:00.510 に答える