BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
DAppelbaum83
Fluorite | Level 6

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? 

1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

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='.';

View solution in original post

3 REPLIES 3
andreas_lds
Jade | Level 19

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='.';
DAppelbaum83
Fluorite | Level 6

Worked perfectly, thank you!

DAppelbaum83
Fluorite | Level 6
Quick follow up, would you know how to display the % of total in addition to the count?

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1638 views
  • 1 like
  • 2 in conversation