2017-05-06 3 views
0

私は次のことを行うには、データ・ステップまたはSQLステートメントをしたいです。SAS - IDでグループ化すると、すべての値を1行の最小値に変換するにはどうすればよいですか?

この表考えてみましょう:、どのように私はdlenfol列内の最小値にdlenfolのすべての値を変換し、rlenfolの全ての値ですidで

(前)

id div dlenfol repurch rlenfol 
1 0 145  1   25 
2 0 114  0   114 
2 0 114  0   114 
3 0 189  1   53 
3 0 189  0   189 
3 1 149  0   189 
4 1 14  0   182 
4 0 182  1   46 
4 0 182  0   182 

グループ化rlenfol列の最小値に?

一方、私もその選択という変数を作成したい:

id div dlenfol repurch rlenfol choice 
1 0 145  1 25   0 
2 0 114  0 114  . 
2 0 114  0 114  . 
3 0 149  1 53   1 
3 0 149  0 53   1 
3 1 149  0 53   1 
4 1 14   0 46   1 
4 0 14   1 46   1 
4 0 14   0 46   1 

(後)私がしようとしてきたコードではありません。このよう

=1 if a certain id EVER had a div=1; 
=0 if a certain id EVER had a repurch=1 (but never had a div=1); 
=1 if a certain id EVER had a div=1 AND EVER had a repurch=1; 
and =. if the certain id never had a div=1 nor repurch=1. 

すなわち作業:

data comb2d; 
set comb; 
do; 
    set comb; 
    by id; 
    dmin = min(dlenfol, dmin); 
    rmin = min(rlenfol, rmin); 
    if dlenfol=dmin and rlenfol^=rmin then CHOICE=1; 
    else if dlenfol^=dmin and rlenfol=rmin then CHOICE=0; 
    else if dlenfol=dmin and rlenfol=rmin then CHOICE=1; 
    else CHOICE=.; 
    /* if DIV=1 and REPURCH=0 then CHOICE=1; 
    else if DIV=0 and REPURCH=1 then CHOICE=0; 
    else if DIV=1 and REPURCH=1 then CHOICE=1; 
    else CHOICE=.; */ 
    end; 
    dlenfol = dmin; 
    rlenfol = rmin; 
    /* drop dmin; 
    drop rmin; */ 
run; 

次のSQLコードは、最小値の問題を解決するようだが、それは私が本当に必要としないという2つの変数(dminのとRMIN)を作成します。

proc sql; 
create table comb3 as 
select *, min(dlenfol) as dmin, min(rlenfol) as rmin 
from comb 
group by comb.id; 
quit; 

答えて

0
proc sort data=before out=sort1; 
by id dlenfol; 
run; 

data sort1; 
    drop temp; 
set sort1; 
retain temp; 
    by id; 
     if first.id then temp = dlenfol; 
     else dlenfol = temp; 
run; 

then do the same thing for rlenfol 
+0

こんにちはDCR。あなたの答えをありがとうが、dlenfolのエントリのほとんどの値が欠落しています。 dlenfolのすべての値をdlenfol列の最小値に変換する必要があることに注意してください。また、Choice変数の作成方法についてのアイデアはありますか?ご協力いただきありがとうございます。そのことについて申し訳ありません –

+0

は、私たちは、私はいくつかのSQLコードを思い付い保持声明 – DCR

+0

を追加する必要があるが、それは私が本当に必要としない2つの新しい変数(dminとし、RMIN)を作成します:PROCのSQLを、 選択*としてテーブルCOMB3を作成し、Dminのような分(dlenfol)、分(rlenfol)comb.gvkeyによって櫛 群からRMIN として。 終了します。 –

0

どのようなものについて:

proc sql; 
create table comb3 as 
select id, div , repurch , min(dlenfol) as dlenfol, min(rlenfol) as relenfol 
from comb 
group by comb.id; 
quit; 
+0

私はうまく動作しませんし、それはまた、不思議にも並べ替えの問題に苦しんでいます。以下は、良い作品(と私はちょうど余分な2つの変数(dminのとRMIN)を維持する必要がありますことを決めましたが、選択変数のコードはまだ機能していない「のprocのSQL​​を、 が 選択*としてテーブルCOMB3を作成し、選択肢として コームから グループによってcomb.gvkey; で注文します。comb.gvkey、c​​omb.fyear; quit; ' –

0

次のコードは現在動作しているようだ:

proc sql; 
create table comb3 as 
select *, min(dlenfol) as dmin, min(rlenfol) as rmin, max(choice) as choicemax 
from comb 
group by comb.gvkey 
order by comb.gvkey, comb.fyear; 
quit; 
0

これを扱うことができ、問題の良い例です。二重DOWループによって。最初のループで、最小値を見つけ、フラグ変数が真であるかどうかをチェックします。 新しいCHOICE変数を定義し、変数を最小値に設定するために必要な情報があります。

data want ; 
    do until(last.id); 
    set have ; 
    by id ; 
    mind=min(mind,dlenfol); 
    minr=min(minr,rlenfol); 
    anydiv=anydiv or div; 
    anyrep=anyrep or repurch; 
    end; 
    if anydiv then choice=1; 
    else if anyrep then choice=0; 
    else choice=.; 
    do until(last.id); 
    set have; 
    by id; 
    dlenfol=mind; 
    rlenfol=minr; 
    output; 
    end; 
    drop mind minr anydiv anyrep; 
run; 
+0

こんにちはTomさん、こんにちは。あなたの答えのために。上記の私のprocのSQL​​コードがうまくいくようだが、私はあなたの解決策もありがとう。少し違う質問をすることができますか?Repurch前1年でDiv = 1ならChoice = = 1の場合(あるIDのDiv = 1とRepurch = 1のいずれかの点で)、同様に、Div = = 1より前の1年でRepurch = 1の場合は、Choice = 0にする必要があります。それを行う方法のアドバイス? –

関連する問題