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

Dear All,
I have one question on random selection of 1 from 2.
Let say a person has 2 eyes and we randomly select only one eye i.e. either left or right.
Are we actually able to get the same eye for everyone every time we run our program? 

Apologies that it's not SAS-related.

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hello @Miracle,

 

It depends on whether you specify a positive random seed.

 

Example:

data want;
call streaminit(27182818);
set sashelp.class;
length random_eye $5;
random_eye=choosec(rand('table',0.5),'left','right');
run;

Thanks to the CALL STREAMINIT statement with a positive argument, the 19 assignments to variable random_eye will be the same every time you run the program. Change the argument to a different positive value (e.g., 1) and you'll get (in general) a different, but again stable set of assignments. Without the CALL STREAMINIT statement or using 0 as its argument the assignments will (in general) change from run to run.

View solution in original post

5 REPLIES 5
FreelanceReinh
Jade | Level 19

Hello @Miracle,

 

It depends on whether you specify a positive random seed.

 

Example:

data want;
call streaminit(27182818);
set sashelp.class;
length random_eye $5;
random_eye=choosec(rand('table',0.5),'left','right');
run;

Thanks to the CALL STREAMINIT statement with a positive argument, the 19 assignments to variable random_eye will be the same every time you run the program. Change the argument to a different positive value (e.g., 1) and you'll get (in general) a different, but again stable set of assignments. Without the CALL STREAMINIT statement or using 0 as its argument the assignments will (in general) change from run to run.

PGStats
Opal | Level 21

SAS provides pseudo-random number generating functions. These functions can be set to start the random number sequence at a given point, determined by a seed value. For example the program

data eyes;
call streaminit(852559); /* Set the random number generation seed */
set sashelp.class;
selectedEye = rand("integer", 2); /* Generate a random number */
run;

proc print; run;

should give you this same pseudo-random sequence :

 

image.png

PG
ballardw
Super User

If I recall correctly, the ability to duplicate the random number stream is somewhat software and hardware dependent. If you run the code on a different version of SAS, or on a machine with a different math co-processor the results may differ.

Ksharp
Super User

Calling @Rick_SAS 

Miracle
Barite | Level 11

Thank you all for your response @FreelanceReinh@PGStats@ballardw  and @Ksharp.
I have used the call streaminit and it gives me the same eye all the time.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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