BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sasecn
Quartz | Level 8

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,

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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;
PG

View solution in original post

8 REPLIES 8
PeterClemmensen
Tourmaline | Level 20

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;
sasecn
Quartz | Level 8

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.

PeterClemmensen
Tourmaline | Level 20

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;
sasecn
Quartz | Level 8

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, ....

PGStats
Opal | Level 21

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;
PG
sasecn
Quartz | Level 8

Thanks, Would like to mark this as solution as well, bu the system won't let me.

Reeza
Super User
Please show what your input data looks like and what you expect for output. Thanks!
sasecn
Quartz | Level 8

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 1659 views
  • 17 likes
  • 4 in conversation