SAS数据步骤:即时将字符串连接到变量

时间:2013-08-06 14:33:38

标签: sas

我有以下示例数据:

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变量。问题是我正在使用字符串变量或者我正在尝试引用前一行中的某些内容吗?

感谢您提供任何帮助。

1 个答案:

答案 0 :(得分:2)

一些问题 - 首先,连接操作(resulthistory=resulthistory||winner)用空格填充,意味着“赢家”被删除了字符串的末尾

在第一个数据步骤中还存在一个不存在的变量(winlose),拼写错误(bedate)和不必要的retain语句。请参阅以下更新代码:

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;