Proc Freq. Create table for all variables

Accepted Solution Solved
Reply
Super Contributor
Posts: 371
Accepted Solution

Proc Freq. Create table for all variables

Hi Everyone,

I want to run proc freq to create a table with each column reporting frequency of each variable in the original dataset.


My original data variables only take value 1, 2 ,3. So it will be quite convenient to get that summary table since it only have 3 row (1,2,3)

I guess there should be a way to do it rather than getting table one by one and combine them with proc sql.

The output should be in the format:

value       var1    var2    var3

1

2

3

Thank you for your help.

HHC

data have;

input var1 var2 var3

datalines;

1 1 1

2 1 2

3 1 2

1 2 3

;run;


Accepted Solutions
Solution
‎08-18-2014 01:27 PM
Respected Advisor
Posts: 4,969

Re: Proc Freq. Create table for all variables

It might take a little reshaping of your data, but it's not lengthy.

data reshape;

   set have;

   array vars {5} var1-var5;

   do _i_=1 to 5;

      varname = vname(vars{_i_});

      value = vars{_i_};

      output;

   end;

   keep varname value;

run;

proc tabulate data=reshape;

   class varname value;

   tables value, varname * N=' ' * f=12.;

run;

Personally, I might prefer one variable per row instead of one per column, such as (all else remaining the same):

   tables varname, N * value=' ' * f=12.   PCTN <value> * value=' ' ;

Good luck.

View solution in original post


All Replies
Grand Advisor
Posts: 17,331

Re: Proc Freq. Create table for all variables

proc tabulate will do that to that to a destination, but not sure what the output tables look like.

Grand Advisor
Posts: 10,210

Re: Proc Freq. Create table for all variables

What do you want the output to look like? summary for values Var1,2,3 stacked vertically? horizontally? row headings of 1,2,3 with a column count for each of your variables?

Lots of ways to combine frequency counts but what you want for a final product will affect the approach.

Solution
‎08-18-2014 01:27 PM
Respected Advisor
Posts: 4,969

Re: Proc Freq. Create table for all variables

It might take a little reshaping of your data, but it's not lengthy.

data reshape;

   set have;

   array vars {5} var1-var5;

   do _i_=1 to 5;

      varname = vname(vars{_i_});

      value = vars{_i_};

      output;

   end;

   keep varname value;

run;

proc tabulate data=reshape;

   class varname value;

   tables value, varname * N=' ' * f=12.;

run;

Personally, I might prefer one variable per row instead of one per column, such as (all else remaining the same):

   tables varname, N * value=' ' * f=12.   PCTN <value> * value=' ' ;

Good luck.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 238 views
  • 3 likes
  • 4 in conversation