BookmarkSubscribeRSS Feed
haydn89
Obsidian | Level 7

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.

2 REPLIES 2
Reeza
Super User

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 Smiley Happy

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;

haydn89
Obsidian | Level 7

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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 2 replies
  • 770 views
  • 3 likes
  • 2 in conversation