Hello I would like to get the following output from a proc freq.
My variables of interest are gender age therapy type and drugname. I need counts and percentage.
Can anyone help with the SAS syntax required to get the following output .
Many thanks in advance.
Gender | Age | Therapy Type | Drugname | n | % |
Male | <18 | Branded | a | ||
b | |||||
c | |||||
18-35 | Generic | a | |||
b | |||||
c | |||||
Female | <18 | Branded | a | ||
b | |||||
c | |||||
18-35 | Generic | a | |||
b | |||||
c |
I dont think you can do more than a 2 by table with proc freq. Try looking at proc tabulate for more complex reporting.
EJ
do you have any suggestions on the best way to do this using proc tabulate?
Proc tabulate can get complicated quickly so its best to start off simple and work up to want you need.
Here is a starting point using a built in SAS dataset:
proc tabulate data=sashelp.cars;
class make type drivetrain cylinders;
table make*type*drivetrain*cylinders,(n pctn);
run;
It wont look pretty until you add more options to the code and Im not sure if the stats I listed are the correct ones for want you need (it will do a lot of different percent types ... so you might have to play with that placement).
There is a lot of information out there so if just google "sas proc tabulate example" you can get a lot of examples to help you out.
Hope this helps!
EJ
And here's some fake data using similar variable names...showing a PROC TABULATE solution and a PROC REPORT solution. You'd have to do a bit more to get the % in TABULATE, but only another format.
Cynthia
**make 45 obs -- some fake data;
data newdata;
infile datalines dlm=',' dsd;
input gender $ age Therapy_type $ drug $ num1 num2;
return;
datalines;
"M",11,"Branded","A",59.00,102.500
"M",12,"Branded","A",65.90,117.125
"M",13,"Branded","A",55.50,74.000
"M",14,"Branded","A",52.15,97.920
"M",14,"Generic","A",69.00,112.500
"M",24,"Generic","A",75.90,127.125
"F",13,"Branded","A",56.50,84.000
"F",23,"Branded","A",62.15,94.920
"F",13,"Branded","A",65.30,98.000
"F",23,"Branded","A",71.83,110.740
"F",14,"Generic","A",62.80,102.500
"F",24,"Generic","A",69.08,115.825
"M",14,"Generic","B",63.50,102.500
"M",24,"Generic","B",69.85,115.825
"M",12,"Branded","B",57.30,83.000
"M",22,"Branded","B",63.03,93.790
"F",12,"Branded","B",59.80,84.500
"F",22,"Branded","B",65.78,95.485
"F",15,"Generic","B",62.50,112.500
"F",25,"Generic","B",68.75,127.125
"M",13,"Branded","B",62.50,84.000
"M",23,"Branded","B",68.75,94.920
"M",12,"Branded","B",59.00,99.500
"M",34,"Generic","A",69.00,112.500
"M",39,"Generic","A",75.90,127.125
"M",33,"Branded","A",56.50,84.000
"M",22,"Branded","B",64.90,112.435
"F",11,"Branded","B",51.30,50.500
"F",21,"Branded","B",56.43,57.065
"F",14,"Generic","B",64.30,90.000
"F",24,"Generic","B",70.73,101.700
"F",12,"Branded","C",56.30,77.000
"F",22,"Branded","C",61.93,87.010
"F",15,"Generic","C",66.50,112.000
"F",25,"Generic","C",73.15,126.560
"M",16,"Generic","C",72.00,150.000
"M",26,"Generic","C",79.20,169.500
"M",12,"Branded","C",64.80,128.000
"M",22,"Branded","C",71.28,144.640
"M",15,"Generic","C",67.00,133.000
"M",25,"Generic","C",73.70,150.290
"M",11,"Branded","C",57.50,85.000
"M",21,"Branded","C",63.25,96.050
"M",15,"Generic","C",66.50,112.000
"M",25,"Generic","C",73.15,126.560
;
run;
title;
proc format;
value afmt 11-16 = '<18'
17-40 = '18-over';
run;
proc sort data=newdata out=newdata;
by gender age therapy_type drug;
run;
ods _all_ close;
ods html file='c:\temp\solution_2_method.html';
proc tabulate data=newdata;
title 'Proc Tabulate Solution';
class gender age therapy_type drug/ missing;
table gender * age* therapy_type*drug all='Total',
n pctn / ;
format age afmt.;
run;
proc report data=newdata nowd;
title 'Proc Report Solution';
column gender age therapy_type drug n pctn;
define gender / group;
define age / group f=afmt. order=data;
define therapy_type / group;
define drug / group;
define n / 'N';
define pctn / 'PctN' f=percent9.2;
rbreak after / summarize;
run;
ods html close;
Proc freq does the same, the key is using proc report to display it the way you want it to be displayed.
proc freq data=newdata noprint;
table gender*age*therapy_type*drug/out=sample outpct;
format age afmt.;
run;
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.