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.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

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