Sas assign random number to each row

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for Search instead for Did you mean: Bookmark Subscribe RSS Feed

🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.

Fluorite | Level 6 Assigning a random numeric ID within a series of numbers Posted 08-07-2012 05:48 PM (15529 views)

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 Obsidian | Level 7 Re: Assigning a random numeric ID within a series of numbers Posted 08-08-2012 04:57 PM (20858 views) | In reply to HyunJee

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
*/