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.
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.
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.
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 :
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.
Calling @Rick_SAS
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.
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!
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.