Hello,
I would like to know if there is a way to round the results with proc freq. Here an example of my data:
data have;
input
id time1 event1 weight var1;
datalines;
1 0 0 0.89595 0
1 1 0 0.89595 0
1 2 0 0.89595 0
1 3 0 0.89595 0
1 4 0 0.89595 0
1 5 0 0.89595 0
1 6 0 0.89595 0
1 7 0 0.89595 0
1 8 0 0.89595 0
1 9 0 0.89595 0
1 10 0 0.89595 0
1 11 0 0.89595 0
1 12 0 0.89595 0
1 13 0 0.89595 0
2 0 0 1.124591 1
2 1 1 1.124591 1
2 2 . 1.124591 1
3 0 0 1.15698 1
3 1 0 1.15698 1
3 2 1 1.15698 1
3 3 . 1.15698 1
4 0 1 0.987654 2
4 1 . 0.987654 2
4 2 . 0.987654 2
4 3 . 0.987654 2
4 4 . 0.987654 2
5 0 0 1.13999 2
6 0 0 1.0503 0
6 1 0 1.0503 0
6 2 0 1.0503 0
6 3 0 1.0503 0
6 4 0 1.0503 0
6 5 1 1.0503 0
6 6 . 1.0503 0
6 7 . 1.0503 0
6 8 . 1.0503 0
7 0 0 0.89214 1
7 1 0 0.89214 1
7 2 0 0.89214 1
7 3 0 0.89214 1
7 4 0 0.89214 1
7 5 0 0.89214 1
7 6 0 0.89214 1
7 7 0 0.89214 1
7 8 1 0.89214 1
7 9 . 0.89214 1
7 10 . 0.89214 1
8 0 0 1.11111 2
8 1 0 1.11111 2
8 2 0 1.11111 2
8 3 . 1.11111 2
8 4 . 1.11111 2
;
run;
So I use proc freq like this:
proc freq data=have;
tables event1*var1;
weight weight;
run;
I would like to round the outputs to the base 5 and to two decimal digits.
Does anyone know how to round frequencies like that ?
Many thanks in advance!
Hello @MFraga,
@MFraga wrote:
I would like to round the outputs to the base 5 and to two decimal digits.
Do you mean rounding to multiples of 0.05?
Example:
ods output crosstabfreqs=ctf;
proc freq data=have;
tables event1*var1;
weight weight;
run;
data freqs;
set ctf;
rfreq=round(frequency,.05);
run;
proc print data=freqs;
var event1 var1 _type_ rfreq;
run;
If you want to retain the cross-tabulation layout from PROC FREQ, you can apply PROC REPORT (or PROC TABULATE) to dataset FREQS (if not to dataset HAVE from the start).
Simple example:
proc format;
value total (default=6)
.='Total';
run;
proc report data=freqs missing;
column event1 var1,rfreq;
define event1 / group order=data f=total.;
define var1 / across order=data f=total.;
define rfreq / ' ' f=6.2;
run;
Result:
var1 0 1 2 Total event1 0 17.80 10.60 4.45 32.85 1 1.05 3.15 1.00 5.20 Total 18.85 13.75 5.45 38.05
It looks like you were lucky regarding rounding errors in the totals. 🙂
You have three options:
1. Create a custom format with the ROUND option and/or a function
2. Round after the fact in a table to ensure totals match (random rounding - more complicated process). Usually what's done by professional organizations though.
3. Modify the PROC FREQ template to do this - I would not really recommend this, though it's possible.
I think Option 1 and 2 are likely what you should try and I suspect it will end up being number 2. So you'll likely need to create a macro that will round the numbers and then correct the totals or other cells as needed to ensure that things are correct. We usually call this random rounding. This is not a trivial task...unless you just want to round everything to nearest 5 and then recalculate totals.
@MFraga wrote:
Hello,
I would like to know if there is a way to round the results with proc freq. Here an example of my data:
data have;
input
id time1 event1 weight var1;
datalines;
1 0 0 0.89595 0
1 1 0 0.89595 0
1 2 0 0.89595 0
1 3 0 0.89595 0
1 4 0 0.89595 0
1 5 0 0.89595 0
1 6 0 0.89595 0
1 7 0 0.89595 0
1 8 0 0.89595 0
1 9 0 0.89595 0
1 10 0 0.89595 0
1 11 0 0.89595 0
1 12 0 0.89595 0
1 13 0 0.89595 0
2 0 0 1.124591 1
2 1 1 1.124591 1
2 2 . 1.124591 1
3 0 0 1.15698 1
3 1 0 1.15698 1
3 2 1 1.15698 1
3 3 . 1.15698 1
4 0 1 0.987654 2
4 1 . 0.987654 2
4 2 . 0.987654 2
4 3 . 0.987654 2
4 4 . 0.987654 2
5 0 0 1.13999 2
6 0 0 1.0503 0
6 1 0 1.0503 0
6 2 0 1.0503 0
6 3 0 1.0503 0
6 4 0 1.0503 0
6 5 1 1.0503 0
6 6 . 1.0503 0
6 7 . 1.0503 0
6 8 . 1.0503 0
7 0 0 0.89214 1
7 1 0 0.89214 1
7 2 0 0.89214 1
7 3 0 0.89214 1
7 4 0 0.89214 1
7 5 0 0.89214 1
7 6 0 0.89214 1
7 7 0 0.89214 1
7 8 1 0.89214 1
7 9 . 0.89214 1
7 10 . 0.89214 1
8 0 0 1.11111 2
8 1 0 1.11111 2
8 2 0 1.11111 2
8 3 . 1.11111 2
8 4 . 1.11111 2
;
run;
So I use proc freq like this:
proc freq data=have;
tables event1*var1;
weight weight;
run;
I would like to round the outputs to the base 5 and to two decimal digits.
Does anyone know how to round frequencies like that ?
Many thanks in advance!
Thank you for the information. Actually, I do not need to go that far. But it is usefull to know how to do it.
Hello @MFraga,
@MFraga wrote:
I would like to round the outputs to the base 5 and to two decimal digits.
Do you mean rounding to multiples of 0.05?
Example:
ods output crosstabfreqs=ctf;
proc freq data=have;
tables event1*var1;
weight weight;
run;
data freqs;
set ctf;
rfreq=round(frequency,.05);
run;
proc print data=freqs;
var event1 var1 _type_ rfreq;
run;
If you want to retain the cross-tabulation layout from PROC FREQ, you can apply PROC REPORT (or PROC TABULATE) to dataset FREQS (if not to dataset HAVE from the start).
Simple example:
proc format;
value total (default=6)
.='Total';
run;
proc report data=freqs missing;
column event1 var1,rfreq;
define event1 / group order=data f=total.;
define var1 / across order=data f=total.;
define rfreq / ' ' f=6.2;
run;
Result:
var1 0 1 2 Total event1 0 17.80 10.60 4.45 32.85 1 1.05 3.15 1.00 5.20 Total 18.85 13.75 5.45 38.05
It looks like you were lucky regarding rounding errors in the totals. 🙂
Thanks!
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.