拆分字符串并使用SAS创建变量

Splitting string and creating variable using SAS

我有一个字符串 28,16OB4N7L8O4L,它使用两个我已拆分成单独变量的数组。

hrs1 hrs2 hrs3 hrs4 hrs5 hrs6 hrs7    
28   16   1     4   7    8    4

cd1  cd2  cd3  cd4  cd5  cd6  cd7

,    O    B    N    L    O    L

现在我想跨变量总结,如果在上面的例子中字符变量中重复相同的值 'O''L' 重复,在这种情况下我想合并为一个并添加各自的小时

输出应该是:

,  O  B  N  L       -COLUMN  
28 24 1  4  11      -VALUES

这是一个转置为规范化(瘦长格式)的示例。我添加了第二个示例记录。

data have;
  input id hrs1-hrs7 (cd1-cd7) (.);
  cards;
1 28 16 1 4 7 8 4 ,OBNLOL
2 1  2  3 4 5 6 7 AAAABBB
;
run;

data tran (keep=id hr cd) / view=tran ;
  set have ;
  array hrs{*} hrs1-hrs7 ;
  array cds{*} cd1-cd7 ;
  do i=1 to dim(hrs) ;
    hr=hrs{i} ;
    cd=cds{i} ;
    output ;
  end ;
run ;

proc sql ;
  select id, cd, sum(hr)
  from tran
  group by id, cd
  ;
quit ;

Returns:

id  cd
________________
 1  ,         28
 1  B          1
 1  L         11
 1  N          4
 1  O         24
 2  A         10
 2  B         18