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

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

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
  • 8 replies
  • 1266 views
  • 17 likes
  • 4 in conversation