Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Generating unique random values

Status
Not open for further replies.

Medvedeff

Instructor
Feb 12, 2008
29
US
Much like my last post for help, I have another problem that I just cannot solve despite my best efforts. I need to generate random integers bounded on a specifyed interval, but I need them to all be unique. In order to do this I am generating random numbers from a uniform distribution, scaling them and then rounding them to the desired interval.

My goal, at least with the code that I have shown below, is to generate 7 datasets, each containing 5 unique integer values. I dont care if different datasets contain the same values, but each individual dataset needs 5 unique values.

The code below is only part of what I have written, but it is the most relevant to what I am asking about. I can post more of the code if need be. Also, the code that I have below is set in a macro statement, which later gets called in a different macro.


%macro samp;
DROP N;
DO SAMPLE=1 to &n;
DO N=1 to 5;
X=Uniform(33);
Y=(X*9)+1;
RY=Round(Y);
RY2=RY;
IF SAMPLE=&n THEN OUTPUT &name&n;
END;
END;
%mend samp;


I have tried several different things and just cant quite get a solution. This site was so helpful with my last post that I figured I would try again. Thank you for any help in advance.
 
Thus far I have been trying to do this by creating a lag of the RY variable, sorting by the lagRY and then putting a statement such as:

if lagry=ry then return;

Ive tried various versions of the statement in various places, and nothing seems to work. One of the problems is that I need to nest the sorting procedure inside of the macro, but it wont resolve if I do that.

Ive also been trying to put a macro label in, so that I can have a statement such as:

if lagRY=RY then out;

out: n=n-1

The goal being that if lagry = ry, then to stop in the middle of that iteration and strike it from the record by redoing that n'th iteration.

Any ideas?
 
This is the way that I would try to do it. I am not sure I fully understood your desires. If this is not what you need you might be able to add some proc sort nodupkey; commands to filter unwanted duplicates if that is a problem.



Good Luck,
dje



Code below:



data gg;

%macro samp;

DO SAMPLE=1 to 9;
DO N=1 to 5;
X=Uniform(33);
Y=(X*9)+1;
RY=Round(Y);
RY2=RY;output;
END;
END;
%mend samp;

%samp;


data a1;set gg;
where sample=1;


data a2;set gg;
where sample=2;

data a3;set gg;
where sample=3;

data a4;set gg;
where sample=4;

data a5;set gg;
where sample=5;

data kjl;h=4;output;run;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top