If you would rather use a hash solution as Peter suggested, I came up with the following code. While it’s a little harder to digest, it’s probably more efficient than my original code. [pre]
data test;
input @1 num @4 CHAR_1 $2. @7 CHAR_2 $2. @10 CHAR_3 $2. @13 CHAR_4 $2. @16 CHAR_5 $2. @19 CHAR_6 $2.;
DATALINES;
1 f 1 33 52 a b
2 Y
3 33
4 1
5 33 a b c
6 Y
7 18 19 20
8 f 33
9 a j
10 1 44
;
run;
data counts(keep=char counts);
length char $ 2;
declare hash all_chars(suminc: "count");
all_chars.definekey('char');
all_chars.definedone();
do i=1 to 71;
char=strip(i);
all_chars.add();
end;
do char= 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j';
all_chars.add();
end;
count=1;
do while (not eof);
set test end=eof;
array char_{*} char_:;
do i=1 to dim(char_) while (char_{i} ne '');
char=char_{i};
rc=all_chars.find();
end;
end;
do i=1 to 71;
char=strip(i);
rc=all_chars.sum(sum: counts);
output;
end;
do char= 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j';
rc=all_chars.sum(sum: counts);
output;
end;
stop;
run;
[/pre]
... View more