BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Santt0sh
Lapis Lazuli | Level 10

Hi All,

 

I am trying to understand the RANUNI function. Trying to create a dataset using RANUNI  

For Ex. I have a Dataset ACCOUNTS having the below-mentioned variables

ACCT_NUM, AFLAG RF1, RF2, RF3, RF4 rf2cnts

0000000001    0            0     1        1      1       1

0000000004    0            1     1        1      1      1

0000000005    0            0     0        0     1       0 

000000300     0            0    0       0       0        0

 

I am using the below code to get a size and assign 1 rf2,rf3, and rf4 respectively if the value is 1  in a new variable (for eg. rfn) 

data rf2 (drop = x n);

set accounts;

n = ranuni(5);

retain x 1;

if rfcnts eq 1 and x = 10 then do;  * x = 10 number of rf2 needed with 1;

        rf2f = 1;

        x+1;

end;

else do;

   rf2f = 0;

end;

run;

I am trying to understand when I run the same data step on the same dataset will the value of N change, have tried running the data step on the same dataset a couple of times but every 10-15 mins I still see the value of N is same. is this how it should be or change every time I run the data step with RANUNI in it and on the same dataset with the same values and size?

 

Kindly note I don't want to use SurveySelect, please see the output attached where we have unchanged values of N.

 

Kindly suggest.

 

Regards,

S

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hi @Santt0sh,

 

If you want n to change in virtually every run of the program, you can use the seed 0, i.e., n = ranuni(0). Then "the time of day is used to initialize the seed stream" (documentation), hence the automatic change. From the resulting random numbers (if you do not drop n from dataset RF2) you can even determine a non-zero seed that can be used to reproduce the same results, if needed.

 

(See also: Six reasons you should stop using the RANUNI function to generate random numbers.)

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

If you run the data step using the same SEED you should get the set series of random numbers.

If that is not what you want then use a different SEED value for each run.

FreelanceReinh
Jade | Level 19

Hi @Santt0sh,

 

If you want n to change in virtually every run of the program, you can use the seed 0, i.e., n = ranuni(0). Then "the time of day is used to initialize the seed stream" (documentation), hence the automatic change. From the resulting random numbers (if you do not drop n from dataset RF2) you can even determine a non-zero seed that can be used to reproduce the same results, if needed.

 

(See also: Six reasons you should stop using the RANUNI function to generate random numbers.)

Santt0sh
Lapis Lazuli | Level 10
Thank You for your quick response and this was helpful.


SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 3 replies
  • 853 views
  • 6 likes
  • 3 in conversation