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
What do you mean by "The remaining random ID"?
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;
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;
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.
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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.