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?

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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