DATA Step, Macro, Functions and more

Randomly Assign a Vendor to Each Row in a Table

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 17
Accepted Solution

Randomly Assign a Vendor to Each Row in a Table

Hi, 

 

I have an interesting question that I would like to pose to you all.

 

I have a table of data that I pulled from an oracle server, and I need to randomly assign a vendor to each row of that table. I currently have a list of 5 vendors and each vendor has the same naming convention (all vendors are named by VENDOR### for example VENDOR001). However, the list of vendors can change at any point of time and either expand or shrink (this will result in a vendor list of VENDOR001 - VENDOR###). 

 

I was thinking of using an array because it will allow for the growing/shrinking of the vendor list. I was also thinking of using ranuni to randomly select an element of the array. For example:

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

/* --OR-- */ 

data test; 
	array _Vendor{*} $6. VENDOR001-VENDOR005 ; 
	r = ceil(dim(_Vendor) * ranuni(12345)); 
	random_element = _Vendor{r}; 
	rand_int = rand("Integer", 1, dim(_Vendor));
	value = _Vendor(rand_int);
run; 

However, I am struggling in implementing this method and I am curious if there is a more straight forward way. To clarify, I need to generate the vendor table and then join to the table I created from pulling data from oracle. 

 

Any insight would be much appreciated, if I left out any details that would be necessary to answer this question please let me know. 

 

Thanks 


Accepted Solutions
Solution
‎02-26-2018 11:32 AM
Super User
Posts: 23,293

Re: Randomly Assign a Vendor to Each Row in a Table

Posted in reply to lhsumdalum

Your first method will work, but it's the same issue as before. You don't have a SET statement so no values are really assigned. 

And you're assigning the array value which you've never initialized, you did set the variable name though. You can either initialize the array or use the VNAME to get the variable name.

 

delete_random.JPG

 

View solution in original post


All Replies
Solution
‎02-26-2018 11:32 AM
Super User
Posts: 23,293

Re: Randomly Assign a Vendor to Each Row in a Table

Posted in reply to lhsumdalum

Your first method will work, but it's the same issue as before. You don't have a SET statement so no values are really assigned. 

And you're assigning the array value which you've never initialized, you did set the variable name though. You can either initialize the array or use the VNAME to get the variable name.

 

delete_random.JPG

 

Occasional Contributor
Posts: 17

Re: Randomly Assign a Vendor to Each Row in a Table

Reeza,

Thank you for going above and beyond to assist me with this question - incredibly helpful & much appreciated !
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 97 views
  • 0 likes
  • 2 in conversation