SAS数据步骤:动态将字符串连接到变量

SAS数据步骤:动态将字符串连接到变量,sas,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:

我有以下样本数据:

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;
数据已经存在;
输入用户名$betdate:datetime。奖金;
仅保留用户名日期日期结果;
dateOnly=datepart(betdate);
设置betdate日期时间的格式。;
仅格式化日期ddmmyy8。;
数据线;
播放者1 12NOV2008:12:04:01-10
播放者1 12 Nov2008:19:03:44 50
播放者2 07NOV2008:14:03:33-50
播放者2 05NOV2008:09:00:00-100
跑
过程打印;跑
proc sort data=have;
截止日期;
跑
数据需求;
集有;
用户名dateOnly betdate;
仅保留用户名DateBeDate winnings Winners resulthistory;
如果赢款>0,则赢家='W';

如果winnings有几个问题-首先,连接操作(
resulthistory=resulthistory | | | winner
)用空格填充,这意味着“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;
数据已经存在;
输入用户名$betdate:datetime。奖金;
dateOnly=datepart(betdate);
设置betdate日期时间的格式。;
仅格式化日期ddmmyy8。;
数据线;
播放者1 12NOV2008:12:04:01-10
播放者1 12 Nov2008:19:03:44 50
播放者2 07NOV2008:14:03:33-50
播放者2 05NOV2008:09:00:00-100
跑
proc sort data=have;
用户名dateonly betdate;
跑
数据需求;
集有;
格式ResultyStory$5。;
用户名dateOnly betdate;
保留结果;
如果赢款>0,则赢家='W';

否则,如果赢了很多东西。我改变了
如果first.dateonly然后resulthistory=winner
如果first.username然后resulthistory=winner这样它就是每个玩家的结果历史记录。请欣赏。。请注意,如果您想要5个以上的结果,您将需要resulthistory变量的较长格式:-)我如何将每个后续结果放入新列,即firstBetResult、secondBetResult等。这样,我就可以在PROC表格中对它们进行分组,以查看firstBet、secondBet等的平均赢款。PROC排序数据=want;仅限用户名日期;proc transpose data=want out=want2;仅限用户名日期;var赢家;跑