Hello all,
Wondering if anyone has a more elegant solution to achieve the following tabular output (see pdf attached entitled Goal.pdf). Ignore the values! They are just there as an example.
We have a data set that looks like this (see listing below):
Obs gender country _TYPE_ _FREQ_ var1 var2 var3
1 . 1 1 2 200 2000 20000
2 . 2 1 2 200 2000 20000
3 1 . 2 2 200 2000 20000
4 2 . 2 2 200 2000 20000
It was created using the following sas program:
data g;
do gender=1 to 2;
do country=1 to 2;
var1=100; var2=1000; var3=10000; output;
end;
end;
proc summary data=g;
types gender country;
class gender country;
var var1 var2 var3;
output out=t2 sum=;
proc print;
However, the following tabulate procedure results in missing values in the column headers:
proc tabulate data=t2 missing;
var var1 var2 var3;
class gender country;
table (var1 var2 var3) , gender country / row=float ;
keylabel sum=' ';
One solution is to reorganise the data like this:
data t2;
set t2;
if gender = 1 then level = 1; else
if gender = 2 then level = 2; else
if country = 1 then level = 3; else
if country = 2 then level = 4;
if level in(1 2) then title = 1; else
if level in(3 4) then title = 2;
run;
proc print data = t2;
proc format;
value title_fmt 1='Gender' 2='Country';
value level_fmt 1='Male' 2='Female' 3='Country 1' 4='Country 2';
run;
However, I was wondering if there was an alternative approach? One that is simpler?
The output doesn't have to be created from proc tabulate.
Any advice would be greatly appreciated!
Haydn.
Proc Tabulate works nicely if you flip your data.
The data you show looks to be summarized already. If you show the pre summarized data there may be a way to avoid flipping it altogether.
Hope that helps
data g;
do gender=1 to 2;
do country=1 to 2;
var1=100; var2=1000; var3=10000; output;
end;
end;
run;
data g_flipped;
set g;
array v(3) var1-var3;
do i=1 to 3;
var=vname(v(i));
value=v(i);
output;
end;
drop var1-var3 i;
run;
proc tabulate data=g_flipped;
class gender country var;
var value;
table var='', (gender country)*sum=''*value='';
run;
Ahhh yes! Replace the summary procedure with a data step that similarly summarises the data.
Same amount of coding with much better output. That definitely is better!
Thanks Reeza!
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.