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

I need to Use SAS random number generation functions `RAND()` and a `DO....END loop` to create 100 obs in variable named X then I want to use another DO loop of 500 rounds to generate a total of 500 samples, each with 100 obs. a sample is basically sampling from a standard normal distribution.

I tried the following code but it does not give me what I need:

 

    data A;
    call streaminit(123);   /* set random number seed */
    do i = 1 to 100;
    X = rand("Normal");     /* random number generator */
     output;
    end;
    
    do r = 1 to 500 ;
    if i then X = rand("Normal");
    output;
    end;
    run;

 

Any input will be greatly appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions
hashman
Ammonite | Level 13

@mrahouma:

Do I understand you correctly that you want 50,000 records with random X where the first 100 have I=1, the next 100 have I=2, and so on till the last 100 with I=500? If so, you need to nest the DO loops, like so:  

data want ;                                                                                                                             
  call streaminit(123) ;                                                                                                                
  do I = 1 to 500 ;                                                                                                                     
    do _n_ = 1 to 100 ;                                                                                                                 
      X = rand ("normal") ;                                                                                                             
      output ;                                                                                                                          
    end ;                                                                                                                               
  end ;                                                                                                                                 
run ;     

Kind regards

Paul D.

 

View solution in original post

4 REPLIES 4
Reeza
Super User
I think you're using observations/records here in a confusing fashion.
How many rows and columns are you expecting in your output data set?
mrahouma
Obsidian | Level 7
Thanks for your time. @hashman has replied this. Appreciate your efforts.
hashman
Ammonite | Level 13

@mrahouma:

Do I understand you correctly that you want 50,000 records with random X where the first 100 have I=1, the next 100 have I=2, and so on till the last 100 with I=500? If so, you need to nest the DO loops, like so:  

data want ;                                                                                                                             
  call streaminit(123) ;                                                                                                                
  do I = 1 to 500 ;                                                                                                                     
    do _n_ = 1 to 100 ;                                                                                                                 
      X = rand ("normal") ;                                                                                                             
      output ;                                                                                                                          
    end ;                                                                                                                               
  end ;                                                                                                                                 
run ;     

Kind regards

Paul D.

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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
  • 4 replies
  • 471 views
  • 1 like
  • 3 in conversation