LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (February 2012, week 3)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Fri, 17 Feb 2012 10:47:58 -0800
Reply-To:     Daniel Nordlund <djnordlund@FRONTIER.COM>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         Daniel Nordlund <djnordlund@FRONTIER.COM>
Subject:      Re: random no generator with probability
In-Reply-To:  <CADquvYiB3Zx5KjXZSx4KcCo7KVEjWXa=E2fgMa+ys8pvSCH76g@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

> -----Original Message----- > From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of > sourav ghosh > Sent: Thursday, February 16, 2012 10:32 PM > To: SAS-L@LISTSERV.UGA.EDU > Subject: Re: random no generator with probability > > ok, forgot about the code..let try it with one by one step > > > > step1: > I have 2 dataset A & B both have dimension n*m(both the dataset always > will be > same dimension) > > prob: I want to write a programe where each value like cell(1*1) of A > will reference from cell (1*1) B. > > like in ranuni function > new value=ranuni(old value,seed) > > here old value is cell(1*1) of A and seed is cell (1*1) of B .. so > similarly sequence will happen for all sell of N*M dimension > > step2: > I want a function which will generate either 0 and 1 with following prob. > May be RANBIN function will do, Also the function have the seed option > like > RANBIN(seed,n,p) not sure what will be value for n for each following > cases, > > > > 1. value mod 5 == 0, with p=5/5 add 0, p=0/5 add 5 > 2. value mod 5 == 1, with p=4/5 add 0, p=1/5 add 5 > 3. value mod 5 == 2, with p=3/5 add 0, p=2/5 add 5 > 4. value mod 5 == 3, with p=2/5 add 0, p=3/5 add 5 > 5. value mod 5 == 4, with p=1/5 add 0, p=4/5 add 5 > step3: > > now I will do relace the A(1*1) with the new random value generating with > respect to seed and above probabilies > > Now I think we can think one by one step > >

I usually don't like to give advice where I don't really know what the OP wants to accomplish, or if I don't think they ought to be doing what thye are asking to do. But I have made some of my concerns known, so here is sufficient rope.

You need to use the CALL RANBIN form of the function to do what is wanted. You will need to assign SEED the value from array B[i,j] that is associated with the value A[i,j]. You haven't told us how you are getting or holding those values so I will assume for these purposes that you already have them in arrays. You will need to handle the case where

mod(A[i,j] EQ = 0,

differently than the others because you cna't pass a probability of 0 to rhe ranbin() function. I also am assuming that in you examples VALUE is referring to some A[i,j] and that the 'seed' values in array B are integers. here is some code that will help get you started. I have broken the code into small steps so you can see what I have done.

*--get modulus of value--*; mod_value = mod(A[i,j],5);

*--get value rounded down to nearest multiple of 5--*; round_value = value - mod_value;

*--assign value of B[i,j] to SEED--**; SEED = B[i,j];

*--random_1 is indicator of whether to add 5--*; *--you can't call ranbin with p=0, so only call when mod_value>0--*; *-- n should be equal to 1, p=mod_value/5--*; random_1 = 0; if (mod_value > 0) then call ranbin(SEED, 1, mod_value/5, random_1);

*--add 5 if indicated by random_1--&; new_value = round_value + 5*random_1;

Now you have new_value which is your replacement value that you can use however you want, including assigning it back to A[i,j]. If you were to put the above code inside two nested do loops, you could iterate through array A.

Just one more caveat. You mentioned in another post to SAS-L

” I need to use single seed for single value. since if you run with a particular seed then each time you will generate same random no..i want that .. some long reason behind this..

so I need a function which will generate random no by seed with probabilities which I have already mention earlier"

You can get the same set of "random" numbers by picking a single seed value. If you read the SAScommunity.org article I pointed you to you would see that from a given starting SEED value, you always get back the same sequence of numbers. So your results would be repeatable starting from a single seed, without the need to store a separate seed for each value you need to process (the qualification is that you need to process the values in the same order if you rerun the code).

Best of luck with your SAS programming.

Dan

Daniel Nordlund Bothell, WA USA


Back to: Top of message | Previous page | Main SAS-L page