I am trying to write a very simple procedure that just counts the distinct values of a given number of fields across a few variables, and tabluates them. PROC FREQ does it for one, but I'm trying to do more than one at once and have them all in a table - haven't had any luck with PROC TABLUATE as it seems to be not for this particular purpose.
For instance, given the below dataset:
data a;
input (A1 A2 A3) ($);
datalines;
A B D
B B D
D A E
;
I would expect some output table like:
A1 A2 A3
A 1 1 0
B 1 2 0
D 1 0 2
E 0 0 1
Is there a way to do that with PROC FREQ or PROC TABLUATE?
You need to transpose the data first, afterwards proc tabulate can create the expected output:
data transposed;
set a;
array _a[3] a1--a3;
length a $ 2 value $ 1;
do i = 1 to dim(_a);
a = vname(_a[i]);
value = _a[i];
output;
end;
drop a1--a3 i;
run;
option missing='0';
proc tabulate data=transposed;
class a value;
table value= ' ', a= ' ';
keylabel n = ' ';
run;
options missing='.';
You need to transpose the data first, afterwards proc tabulate can create the expected output:
data transposed;
set a;
array _a[3] a1--a3;
length a $ 2 value $ 1;
do i = 1 to dim(_a);
a = vname(_a[i]);
value = _a[i];
output;
end;
drop a1--a3 i;
run;
option missing='0';
proc tabulate data=transposed;
class a value;
table value= ' ', a= ' ';
keylabel n = ' ';
run;
options missing='.';
Worked perfectly, thank you!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.