1

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

Joe
Joe
John
Bill
Bill
Bill
Bill
Joanne
Joanne
Fred
Fred
Fred
Ted

私がやろうとしているのは、その変数の「ストリーク」を格納する変数を追加して、各エントリを一度だけ持つ新しいデータセットにすることです。たとえば、セットは次のようになります。

Joe     2
Bill    4
Joanne  2
Fred    3
Ted     1

これまでのところ、最初のデータ セットを作成しましたが、2 番目のデータ セットの作成方法に行き詰まっています。私の考えは、比較のために最後の観測値を保持するデータ ステップで変数を作成することですが、その方法、またはそれを 2 番目のデータ セットに変換する方法がわかりません。

SASで以前の観察に戻ることさえ可能かどうかはわかりません。おそらくこれは a の問題ですが、テーブルではなくproc sql新しいものが必要です。data set

4

1 に答える 1

4

名前が 1 回だけ出現する場合、つまり、bill、bill、joe、joe、bill、bill というストリークを持つことができない場合は、proc freq を使用します。

proc freq data=have;
table name/out=want;
run;

それ以外の場合は、カウンターで notsorted オプションを使用します。

data want;
set have;

*BY tells sas my data has groups according to the "Name" variable , 
with the notsorted meaning that it isn't an alphabetical or numerical order
basically, whenever the Name changes its a new group;
by name notsorted;

*Keep the value across the different rows rather than reset;
retain counter;

*if this is the first of a group then set the counter to 1;
if first.name then counter=1;
*If this isn't the first name in the group then increment the counter,
retain means its kept the value from previous row;
else counter+1;

*If this is the last of the "Name" group then output the observation;
if last.name then output;
run;
于 2014-10-02T14:59:35.957 に答える