BookmarkSubscribeRSS Feed
lhsumdalum
Obsidian | Level 7

Hi, 

 

I have an array of length n, for example : 

 

ARRAY _Example{*} 6$ EXA001: 

I want to randomly select an element of the array. Is there an easy or straight forward way of achieving this? 

 

The larger scope of this issue is that I want to assign a random element of the the array (map the element for example EXA004) to a dataset. Essentially, randomly assign one of the elements of the array to an ID variable. 

 

Any insight into this issue would be much appreciated. 

 

Cheers. 

 

8 REPLIES 8
Astounding
PROC Star

Assuming that the syntax for the array statement gets fixed ...

 

data want;

set have;

array _Example {*} whatever goes here;

r = ceil( dim(_Example) * ranuni(12345) ) ;

randomly_selected_element = _Example{r};

run;

 

The variable R will be a randomly selected integer, minimum is 1 and maximum is number of elements in the array.

lhsumdalum
Obsidian | Level 7

The test that I tried with this logic will not populate the random_element column with the array element. 

 

data test; 
	array _Example{*} $6. TEST01-TEST10 ; 
	r = ceil(dim(_Vendor) * ranuni(12345)); 
	random_element = _Example{r}; 
run; 

Random_Element.PNG

 

I appreciate your help, please bear with me as I am a new SAS user.

 

Reeza
Super User

You don't have a SET statement. Don't you have an input dataset?

lhsumdalum
Obsidian | Level 7
I do have an input dataset that I want to map the elements of the value column in the above example to. However, I needed to create the array myself. I was planning on using a proc sql to join the elements of the value column to the existing data set.
Reeza
Super User

You lost me now. 

 

I still suspect you need to add the SET statement. 

 

OR if you're creating a new data set from scratch you need a loop with an output statement.

 

do i=1 to 1000; *number of records;
x= .... ;
output;
end;

 

 

Reeza
Super User

1. Generate a random uniform variable between 1 and 6. This assumes you're using SAS 9.4 TS1M4+, if you don't, use the approach outlined here

 

rand_int = rand('Integer', 1, 6);

2. Use that index in the array to map the value over. 

 

value = _example(rand_int);

@lhsumdalum wrote:

Hi, 

 

I have an array of length n, for example : 

 

ARRAY _Example{*} 6$ EXA001: 

I want to randomly select an element of the array. Is there an easy or straight forward way of achieving this? 

 

The larger scope of this issue is that I want to assign a random element of the the array (map the element for example EXA004) to a dataset. Essentially, randomly assign one of the elements of the array to an ID variable. 

 

Any insight into this issue would be much appreciated. 

 

Cheers. 

 


 

lhsumdalum
Obsidian | Level 7

Is it possible to replace the hard-coded upper bound of 6 with the unspecified dimension of the array? To continue your example, as such:

rand_int = rand('Integer', 1, dim(_Example));
Reeza
Super User

I don't know, did it work?


@lhsumdalum wrote:

Is it possible to replace the hard-coded upper bound of 6 with the unspecified dimension of the array? To continue your example, as such:

rand_int = rand('Integer', 1, dim(_Example));

 

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 3941 views
  • 3 likes
  • 3 in conversation