## counting horizontally .....

Regular Contributor
Posts: 229

# counting horizontally .....

I should get the below output from the given dataset "X" .
data x;
input id a\$ b\$ c\$ d\$ e\$ f\$ g\$;
cards;
1 B B B C C C P
3 B B C C C C P
4 C C C C C P P
5 B C C P P P P
6 P P P P P P P
7 C C C C P P P
run;

output:

id count(B) count(C) count(P)
1 3 3 1
3 2 4 1
4 0 5 2
....
.
. Message was edited by: Michel_SAS
Regular Contributor
Posts: 229

## Re: counting horizontally .....

got it

data y(drop= a--g i);
set x;
array arr
• _character_;
bcount=0;Ccount=0;Pcount=0;
do i=1 to dim(arr);
select (arr);
when ("B") Bcount=Bcount+1;
when ("C") Ccount=Ccount+1;
when ("P") Pcount=Pcount+1;
end;
end;
run;
• Regular Contributor
Posts: 229

## Re: counting horizontally .....

data y(drop=x--r i );
set x;
bounce_string=cat(a,b,c,d,e,f,g);
BCount=0;CCount=0;PCount=0;
do i=1 to length(bounce_string);
select(substr(bounce_string,i,1));
when ("b") BCount=Bcount+1;
when ("p") PCount=Pcount+1;
when ("c") CCount=CCount+1;
otherwise;
end;
end;
run;
proc print;
run; Message was edited by: Michel_SAS
Posts: 3,852

## Re: counting horizontally .....

Try the COUNT function.

[pre]
data x;
input id a\$ b\$ c\$ d\$ e\$ f\$ g\$;
array v
• a b c d e f g;
string = cats(of v
• );
bCount = count(string,'B');
cCount = count(string,'C');
pCount = count(string,'P');
cards;
1 B B B C C C P
3 B B C C C C P
4 C C C C C P P
5 B C C P P P P
6 P P P P P P P
7 C C C C P P P
;;;;
run;
proc print;
run;
[/pre]
• Discussion stats
• 3 replies
• 153 views
• 0 likes
• 2 in conversation