2

次のサンプルデータがあります。

data have;
   input username $  betdate : datetime. winnings;
   retain username dateonly bedate result;
   dateOnly = datepart(betdate) ;
   format betdate DATETIME.;
   format dateOnly ddmmyy8.;
   datalines; 
    player1 12NOV2008:12:04:01 -10
    player1 12NOV2008:19:03:44 50
    player2 07NOV2008:14:03:33 -50
    player2 05NOV2008:09:00:00 -100
run;
PROC PRINT; RUN;
proc sort data=have;
   by username betdate;
run;
data want;
   set have;
    by username dateOnly betdate;   
   retain username dateonly bedate winnings winner resulthistory;
   if winnings > 0 then winner = 'W';
   if winnings <= 0 then winner = 'L';
   if first.winlose then resulthistory=winner;
   else if first.betdate then resulthistory=resulthistory||winner;
 PROC PRINT; RUN;

最後の列に累積結果履歴が必要です。player1 の場合、これは「WL」になります。player2 の場合は 'LL' にする必要があります。2 番目のデータ ステップで resulthistory 変数を宣言しましたが、同じユーザー名の場合、新しい結果を resulthistory 変数に連結できないようです。問題は、文字列変数を使用していることですか、それとも前の行から何かを参照しようとしていることですか?

これについて助けてくれてありがとう。

4

1 に答える 1

2

いくつかの問題 - まず、連結アクション ( resulthistory=resulthistory||winner) に空白が埋め込まれていたため、「勝者」が文字列の末尾から切り落とされていました。

また、存在しない変数 (winlose)、タイプミス (bedate)、および最初のデータ ステップでの不要な保持ステートメントもありました。以下の更新されたコードを参照してください。

data have;
  input username $ betdate : datetime. winnings;
  dateOnly = datepart(betdate);
  format betdate DATETIME.;
  format dateOnly ddmmyy8.;
datalines;
player1 12NOV2008:12:04:01 -10
player1 12NOV2008:19:03:44 50
player2 07NOV2008:14:03:33 -50
player2 05NOV2008:09:00:00 -100
run;

proc sort data=have;
  by username dateonly betdate;
run;
data want;
  set have;
  format resulthistory $5.;
  by username dateOnly betdate;
  retain resulthistory;
  if winnings > 0 then winner = 'W';
  else if winnings <= 0 then winner = 'L';
  if first.dateonly then resulthistory=winner;
  else resulthistory=cats(resulthistory,winner);
run;
PROC PRINT; RUN;
于 2013-08-06T14:44:58.160 に答える