BookmarkSubscribeRSS Feed
jlcalbre
Calcite | Level 5
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%
6 REPLIES 6
ChrisNZ
Tourmaline | Level 20
This should help:

proc tabulate data=SASHELP.CLASS;
var WEIGHT HEIGHT;
class AGE;
table AGE, (WEIGHT HEIGHT)*pctsum;
run;
jlcalbre
Calcite | Level 5
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.
Bill
Quartz | Level 8
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;
jlcalbre
Calcite | Level 5
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.
RPGarland
Calcite | Level 5
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;
Cynthia_sas
SAS Super FREQ
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]

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 6 replies
  • 885 views
  • 0 likes
  • 5 in conversation