I would like to round numbers (e.g. count of something) by probability:
If the count is a multiple of 3 with a remainder 0, it stays the same
If the count is a multiple of 3 with a remainder 1, it has 1/3 probability to round up, and 2/3 probability to round down.
If the count is a multiple of 3 with a remainder 2, it has 2/3 probability to round up, and 1/3 probability to round down.
I only know using floor and ceil to round up and down, but don't know how to use the probability.
Thanks,
For something more general, you could use:
data have;
do x=1 to 100;
output;
end;
run;
%let base=5;
data want;
set have;
alpha = 1 - mod(x, &base) / &base;
value = &base * int(x / &base);
rx = choosen( rand("table", alpha), value, &base + value);
run;
What do you want to round to? Your numbers are integers, correct?
Here is a template to get you started..
data want;
set have;
r = mod (x, 3);
if r = 0 then rx = x;
else if r = 1 then ..;
else if r = 2 then ..;
run;
Sorry, my question is not clear.
I want to round integer numbers to base of 3. e.g. 4 will be rounded to 6 with prob=1/3, and to 3 with prob=2/3 i.e. if the number (integer) is a multiple of 3 with a remainder 1, it has 1/3 probability to round up to a number base of 3, and 2/3 probability to round down to a number base of 3.
A simple solution
data have;
do x=1 to 10;
output;
end;
run;
data want;
set have;
r = mod (x, 3);
if r = 0 then rx = x;
else if r = 1 then rx = ifn (rand('uniform') < 1/3, x+2, x-1);
else if r = 2 then rx = ifn (rand('uniform') < 1/3, x-2, x+1);
run;
Thanks for your reply. Couple of questions:
Is rand('uniform')<1/3 the same as generating a prob-1/3 ? i know the uniform distribution, but is this a general use of it?
How about a more general solution for different base, e.g what if i want to round to a number of base 5, 10, 100, ....
For something more general, you could use:
data have;
do x=1 to 100;
output;
end;
run;
%let base=5;
data want;
set have;
alpha = 1 - mod(x, &base) / &base;
value = &base * int(x / &base);
rx = choosen( rand("table", alpha), value, &base + value);
run;
Thanks, Would like to mark this as solution as well, bu the system won't let me.
It is just alike the data from draycut's reply. i have just one column of number and want to round it by adding the rounded number beside it. The key is how to round it by probability.
data have;
do x=1 to 10;
output;
end;
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 how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.