BookmarkSubscribeRSS Feed
Smitha9
Fluorite | Level 6

Hi,

I have a data with random ID assigned( created 5digit random ID for 99999).

ID randomID

1    34556

2    45678

3   67873

Now I want to continue assigning the remaining random ID for a different variable. Can I do that?

ID randomID SNO

1     34556       

2     45678

3     67873   

       23564        1

      67856         2

      73456         3       

7 REPLIES 7
PeterClemmensen
Tourmaline | Level 20

What do you mean by "The remaining random ID"?

Smitha9
Fluorite | Level 6
So I created 99999 randomID's and saved it. I am taking the randomID's from the saved ones to assign checkID.
ID checkID S.NO
1 34556
2 45678
3 67873
1
2
3

I want to fill these checkID from the randomID continue.
Kurt_Bremser
Super User

So you need to make sure you do not accidentally use a randomid that is already used?

See this with a hash object:

data have;
input id randomid;
datalines;
1    34556
2    45678
3   67873
;

data new;
input sno;
datalines;
1
2
3
;

data want;
set
  have
  new (in=n)
;
if _n_ = 1
then do;
  declare hash rand ();
  rand.definekey('randomid');
  rand.definedone();
end;
if not n
then rc = rand.add();
else do;
  randomid = rand('integer',1,99999);
  do until (rand.check());
    randomid = rand('integer',1,99999);
  end;
  rc = rand.add();
end;
drop rc;
run;
PeterClemmensen
Tourmaline | Level 20

If I understand your question correctly:

 

data have;
input id randomid sno;
datalines;
1 34556 .
2 45678 .
3 67873 .
. .     1
. .     2
. .     3
;

data want (drop = h r);
   array a {99999} _temporary_ (1:99999);
   retain h 99999;
   set have;
   r = a[rand('integer', 1, h)];
   randomid = coalesce(randomid, r);
   a[randomid] = a[h];
   h +- 1;
run;
Smitha9
Fluorite | Level 6
I created Random ID's lets say: 34567,45678,62398,72345,56183,26532
SO the first 3 I already took it for CheckID. But I want the remaining 3 randomID assigned to S.NO.
I want like this below:
ID checkID S.NO
1 34567
2 45678
3 62398
72345 1
56183 2
26532 3
Patrick
Opal | Level 21

Look at the code @Kurt_Bremser posted. Unlike what you describe this code creates the random numbers on-the-fly and then maintains a "blacklist" of numbers already used. This way you don't even have to pre-generate a list of random numbers.

Rick_SAS
SAS Super FREQ

You posted essentially the same question two months ago and got many responses:

https://communities.sas.com/t5/SAS-Programming/unique-random-Patient-ID/m-p/648204