Help using Base SAS procedures

Creating frequncy Table

Reply
Occasional Contributor
Posts: 9

Creating frequncy Table

I have 2 variables that have values that range from 1 to 5. I want to create a table that simply lists the variable names across the top, each value down the side (basically a category label) and then the column percentages under each variable for each value. This should be pretty simple but I can't figure out how to do it. I tried Proc Tabulate but it keeps asking for another dimiension. Any help is appreciated. The output table would look something like this:

Var1 Var2
1 18% 24%
2 10% 17%
3 13% 19%
4 21% 9%
5 38% 31%
PROC Star
Posts: 1,561

Re: Creating frequncy Table

This should help:

proc tabulate data=SASHELP.CLASS;
var WEIGHT HEIGHT;
class AGE;
table AGE, (WEIGHT HEIGHT)*pctsum;
run;
Occasional Contributor
Posts: 9

Re: Creating frequncy Table

That code splits everything across age. I simply want the values of Var1 and Var2 down the side with their associated frequencies in the table. I don't want to crosstab them with another variable.
Super Contributor
Posts: 291

Re: Creating frequncy Table

if I understand your request correctly, this should do it

data test;
input Var1 Var2;
Row=_N_;
datalines;
18 24
10 17
13 19
21 9
38 31
;
run;

proc tabulate data=test;
class ROW;
var var1 var2;
table row='',var1 var2/rts=3;
run;
Occasional Contributor
Posts: 9

Re: Creating frequncy Table

Not exactly. To further explain, I have 500 observations. The values of var1 and var2 range from 1 to 5. 18% of the values in var1 (n = 90) have a value of 1, 10% (n=50) have a value of 2, etc. If I used proc freq, I would get 2 separate tables that display the value 1 to 5 and the associated counts and percentages with each value. I just want to be able to display the same output from proc freq (percetnages or counts) but for 2 or more variables at the same time in the same table.
Occasional Contributor
Posts: 5

Re: Creating frequncy Table

Well its not a proc freq output but if you want the results in a dataset to tweek as you choose, you can do it this way:


%let total=0;

data counts;
input var1 var2;
call symput('total',_N_);
datalines;
1 2
3 4
5 1
2 3
4 5
1 5
2 3
4 1
5 2
1 3
1 5
4 2
3 1
;
run;
proc sort data=counts;
by var1;
run;

data count1 (keep=var count1 pct1);
set counts;
by var1;
if first.var1 then count1=0;
count1+1;
rename var1=var;
if last.var1 then do;
pct1=100*count1/&total;
output;
end;
run;

proc sort data=counts;
by var2;
run;

data count2 (keep=var count2 pct2);
set counts;
by var2;
if first.var2 then count2=0;
count2+1;
rename var2=var;
if last.var2 then do;
pct2=100*count2/&total;
output;
end;
run;

data count;
merge count1 count2;
by var;
cntpct1=put(count1,best.)||" ("||put(pct1,4.1)||")";
cntpct2=put(count2,best.)||" ("||put(pct2,4.1)||")";
run;

proc print data=count noobs;
var var cntpct1 cntpct2;
run;
SAS Super FREQ
Posts: 8,743

Re: Creating frequncy Table

Hi:
If the data are in a slightly different structure, then you can use TABULATE. The program below is based on the previous scenario for the data. With a TYPE variable and a VAL variable, you can get TABULATE to calculate the COLPCTN statistic automatically.

cynthia

[pre]
data counts(keep=type val);
infile datalines;
input var1 var2;
type='Var1'; val=var1; output;
type='Var2'; val=var2; output;
return;
datalines;
1 2
3 4
5 1
2 3
4 5
1 5
2 3
4 1
5 2
1 3
1 5
4 2
3 1
;
run;

/*
proc print data=counts;
title 'what is the new structure';
run;
*/

proc tabulate data=counts;
class type val;
table val all,
type*colpctn;
keylabel colpctn=' ';
run;
[/pre]
Ask a Question
Discussion stats
  • 6 replies
  • 154 views
  • 0 likes
  • 5 in conversation