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'。我在第二个数据步骤中声明了 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 回答