BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
HyunJee
Fluorite | Level 6

I am trying to figure out if there is a way to automate the process of assigning random IDs to a set of 50 records. I want to randomly assign a number between 1 and 100 to the 50 records - is there a SAS function that do this for me? I have found some random ID creation code, but what I have found creates decimals and alphanumeric IDs and I cannot specify a specific range. Thank you for any help you can provide.

1 ACCEPTED SOLUTION

Accepted Solutions
chang_y_chung_hotmail_com
Obsidian | Level 7

I like patrick's. Here is another using an array in order to keep track of which new id has already been assigned. hth


  /* a test data set with 50 obs */
  data one;
    do oldId = 1 to 50;
      output;
    end;
  run;
 
  /* attach a randomly selected id from [1,100] */
  %let seed=1234567;
  data two;
    array isAssigned[1:100] _temporary_ (100*0);
    set one;
    do while (1);
      newId = ceil(100*ranuni(&seed));
      if isAssigned[newId] then continue;
      isAssigned[newId] = 1;
      leave;
    end;
  run;
 
  /* check */
  proc print data=two;
  run;
  /*
         old    new
  Obs     Id     Id
    1      1     69
    2      2     52
    3      3     90
    4      4      1
  ...
   50     50     45
  */

View solution in original post

5 REPLIES 5
PGStats
Opal | Level 21

Here is a simple way :

/* Create the set of IDs that you want, */
data tmp;
do id = 1 to 100;
     order=ranuni(-1);
     output;
     end;
run;

/* sort them in random order, */
proc sort data=tmp out=ids(keep=id); by order; run;

/* associate the IDs to your observations. */

data want;
set yourData;
set ids;
run;

 

PG

PG
Astounding
PROC Star

Your assumptions and constraints will affect the solution.  Here, I assumed that you want integers only from 1 to 100, and it is OK for two observations to have the same random ID assigned.  If that doesn't reflect what you need, we can revisit:

data want;

   set have;

   id = ceil(100*ranuni(0));

run;

Note that you will not be able to replicate your results this way.  Using 0 as the seed to a random number generator creates a stream of numbers based on the time clock as the program begins.  Again, if that is an issue, we can revisit what you need.

Good luck.

Patrick
Opal | Level 21

If you have SAS/Stat licensed then you could also use PROC PLAN (thanks Data Null; for the time when you've advertised this Proc to us).

I believe below usage of PROC PLAN generates a random row of the numbers 1 to 100 without repetition.

proc plan seed=0;
  factors id=100 /noprint;
  output out=IDs;
run;

data have;
  do MyVar=1 to 50;
    output;
  end;
run;


data want;
  set IDs;
  set have;
run;

chang_y_chung_hotmail_com
Obsidian | Level 7

I like patrick's. Here is another using an array in order to keep track of which new id has already been assigned. hth


  /* a test data set with 50 obs */
  data one;
    do oldId = 1 to 50;
      output;
    end;
  run;
 
  /* attach a randomly selected id from [1,100] */
  %let seed=1234567;
  data two;
    array isAssigned[1:100] _temporary_ (100*0);
    set one;
    do while (1);
      newId = ceil(100*ranuni(&seed));
      if isAssigned[newId] then continue;
      isAssigned[newId] = 1;
      leave;
    end;
  run;
 
  /* check */
  proc print data=two;
  run;
  /*
         old    new
  Obs     Id     Id
    1      1     69
    2      2     52
    3      3     90
    4      4      1
  ...
   50     50     45
  */

HyunJee
Fluorite | Level 6

Thank you Patrick and Chang! The answers you provided were extremely helpful.

Chang -

I am not very familiar with the seed, ceil and ranuni functions in SAS, could you explain a little of what they are performing? the resources I found cleared it up some, but I thought perhaps you may be able to help explain it and help me understand better. Thank you for all of your help.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 5 replies
  • 15057 views
  • 4 likes
  • 5 in conversation