<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Do loop for stimulation in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-for-stimulation/m-p/810988#M319855</link>
    <description>&lt;P&gt;First, thank you for posting the code in a text box - MUCH easier on my eyes - and it preserves the fixed pitch font in the SAS log - which is occasionally important.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have checked on my previous comment&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;I presume the rand('gamma',&amp;amp;NOBs.,1) returns a continuous result in the (0,100] interval - i.e. greater than zero and up through 100.&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I should have done this before, because the &lt;A href="https://support.sas.com/documentation/cdl/en/lefunctionsref/63354/HTML/default/viewer.htm#p170xcnh05pr8gn1vtvi77x35ltt.htm" target="_self"&gt;SAS Functions and Call Routines&lt;/A&gt;&amp;nbsp;documentation for the GAMMA distribution just says the the RAND('GAMMA'... function will produce a positive number.&amp;nbsp; There is no upper limit, so you may very well be generating numbers greater than 100, which could cause the error message you report.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So if you have a strategy of a fixed number (100 in your case) of buckets selected based on the RAND('GAMMA' function, you will have to decide whether 100 buckets (i.e. macrovar NOBS=100) is enough, and what to do about results from RAND('GAMMA' that exceed &amp;amp;NOBS.&amp;nbsp; &amp;nbsp;You could replace the single statement&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;        R[rand('gamma', &amp;amp;Nobs.,1)] + (diff&amp;gt;0);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;with&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;        J=ceil(rand('gamma', &amp;amp;Nobs.,1));
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and then decide whether to add&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;        R[J] + (diff&amp;gt;0);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;depending on the value of J.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Of course, it's not at all clear what your goal is, so you may need to take another strategy.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 02 May 2022 03:11:57 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2022-05-02T03:11:57Z</dc:date>
    <item>
      <title>Do loop for stimulation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-for-stimulation/m-p/810911#M319801</link>
      <description>&lt;P&gt;Hello experts,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I tried to repeat the codes below 200 times but when I add the do loop, SAS says "array subscript out of range". Could you please show me how to achieve what I want? When I change the seed sometimes it works sometimes it does not.&lt;/P&gt;
&lt;P&gt;Many thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%let nobs=100;&lt;/P&gt;
&lt;P&gt;%let nboot=200;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data weight_1;&lt;/P&gt;
&lt;P&gt;do i=1 to &amp;amp;nboot.;&lt;/P&gt;
&lt;P&gt;output;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data weight_2;&lt;/P&gt;
&lt;P&gt;set weight_1 end=lastobs;&lt;BR /&gt;call streaminit (1293);&lt;BR /&gt;do n=1 to &amp;amp;nboot.;&lt;/P&gt;
&lt;P&gt;array R [&amp;amp;Nobs] _temporary_;&lt;/P&gt;
&lt;P&gt;if _N_=1 then do;&lt;/P&gt;
&lt;P&gt;do I=1 to &amp;amp;NObs.;&lt;/P&gt;
&lt;P&gt;R[I}=rand('gamma',1,1);&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;diff=&amp;amp;Nobs. - sum(of r[*]);&lt;/P&gt;
&lt;P&gt;do i=1 to diff;&lt;/P&gt;
&lt;P&gt;R[rand('gamma', &amp;amp;Nobs.,1)] + (diff&amp;gt;0);&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;VET+sum(of R[*]);&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;ran=r[_N_];&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Sun, 01 May 2022 13:15:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-for-stimulation/m-p/810911#M319801</guid>
      <dc:creator>Dunne</dc:creator>
      <dc:date>2022-05-01T13:15:07Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop for stimulation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-for-stimulation/m-p/810920#M319804</link>
      <description>&lt;P&gt;Whenever you get errors in the log, you need to show us the ENTIRE log for this data step. Please show us the ENTIRE log for this DATA step by copying it as text and then pasting it into the window that appears when you click on the &amp;lt;/&amp;gt; icon.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Insert Log Icon in SAS Communities.png" style="width: 859px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/66171iFEC370B1DBF07B28/image-size/large?v=v2&amp;amp;px=999" role="button" title="Insert Log Icon in SAS Communities.png" alt="Insert Log Icon in SAS Communities.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 01 May 2022 14:38:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-for-stimulation/m-p/810920#M319804</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-05-01T14:38:18Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop for stimulation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-for-stimulation/m-p/810923#M319806</link>
      <description>&lt;PRE&gt;1                                                          The SAS System                                  10:43 Sunday, May 1, 2022

1          ;*';*";*/;quit;run;
2          OPTIONS PAGENO=MIN;
3          %LET _CLIENTTASKLABEL='Program';
4          %LET _CLIENTPROCESSFLOWNAME='Standalone Not In Project';
5          %LET _CLIENTPROJECTPATH='';
6          %LET _CLIENTPROJECTPATHHOST='';
7          %LET _CLIENTPROJECTNAME='';
8          %LET _SASPROGRAMFILE='';
9          %LET _SASPROGRAMFILEHOST='';
10         
11         ODS _ALL_ CLOSE;
12         OPTIONS DEV=SVG;
13         GOPTIONS XPIXELS=0 YPIXELS=0;
14         %macro HTML5AccessibleGraphSupported;
15             %if %_SAS_VERCOMP(9, 4, 4) &amp;gt;= 0 %then ACCESSIBLE_GRAPH;
16         %mend;
17         FILENAME EGHTML TEMP;
18         ODS HTML5(ID=EGHTML) FILE=EGHTML
19             OPTIONS(BITMAP_MODE='INLINE')
20             %HTML5AccessibleGraphSupported
21             ENCODING='utf-8'
22             STYLE=HtmlBlue
23             NOGTITLE
24             NOGFOOTNOTE
25             GPATH=&amp;amp;sasworklocation
26         ;
NOTE: Writing HTML5(EGHTML) Body file: EGHTML
27         
28         %let nobs=100;
29         
30         %let nboot=200;
31         
32         
33         
34         data weight_1;
35         
36         do i=1 to &amp;amp;nboot.;
37         
38         output;
39         
40         end;
41         
42         
43         

NOTE: The data set WORK.WEIGHT_1 has 200 observations and 1 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds
      

44         data weight_2;
45         
46         set weight_1 end=lastobs;
47         call streaminit (1293);
48         do n=1 to &amp;amp;nboot.;
49         
50         array R [&amp;amp;Nobs] _temporary_;
2                                                          The SAS System                                  10:43 Sunday, May 1, 2022

51         
52         if _N_=1 then do;
53         
54         do I=1 to &amp;amp;NObs.;
55         
56         R[I}=rand('gamma',1,1);
57         
58         end;
59         
60         
61         
62         diff=&amp;amp;Nobs. - sum(of r[*]);
63         
64         do i=1 to diff;
65         
66         R[rand('gamma', &amp;amp;Nobs.,1)] + (diff&amp;gt;0);
67         
68         end;
69         
70         VET+sum(of R[*]);
71         
72         end;
73         
74         ran=r[_N_];
75         
76         end;
77         
78         run;

ERROR: Array subscript out of range at line 66 column 1.
lastobs=0 i=1 n=2 diff=4.7355896866 VET=105.65851235 ran=0.3109976073 _ERROR_=1 _N_=1
NOTE: The SAS System stopped processing this step because of errors.
NOTE: There were 2 observations read from the data set WORK.WEIGHT_1.
WARNING: The data set WORK.WEIGHT_2 may be incomplete.  When this step was stopped there were 0 observations and 5 variables.
NOTE: DATA statement used (Total process time):
      real time           0.10 seconds
      cpu time            0.03 seconds
      

79         
80         %LET _CLIENTTASKLABEL=;
81         %LET _CLIENTPROCESSFLOWNAME=;
82         %LET _CLIENTPROJECTPATH=;
83         %LET _CLIENTPROJECTPATHHOST=;
84         %LET _CLIENTPROJECTNAME=;
85         %LET _SASPROGRAMFILE=;
86         %LET _SASPROGRAMFILEHOST=;
87         
88         ;*';*";*/;quit;run;
89         ODS _ALL_ CLOSE;
90         
91         
92         QUIT; RUN;
93         
&lt;/PRE&gt;
&lt;P&gt;Thanks for your suggestion. The log is attached. You can also just run the code to see it because it is simulated data.&lt;/P&gt;</description>
      <pubDate>Sun, 01 May 2022 14:45:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-for-stimulation/m-p/810923#M319806</guid>
      <dc:creator>Dunne</dc:creator>
      <dc:date>2022-05-01T14:45:13Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop for stimulation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-for-stimulation/m-p/810924#M319807</link>
      <description>&lt;P&gt;Line 66&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;66         R[rand('gamma', &amp;amp;Nobs.,1)] + (diff&amp;gt;0);&lt;/PRE&gt;
&lt;P&gt;You are creating a value from a gamma distribution, which is the index of array R. Values from a gamma distribution are continuous, they are not necessarily integers, and the index to array R must be an integer between 1 and the maximum size of the array. How to fix this? I have no idea, as I don't know what this line is trying to do. Please explain, not only this line, but what your program is trying to do.&lt;/P&gt;</description>
      <pubDate>Sun, 01 May 2022 15:07:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-for-stimulation/m-p/810924#M319807</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-05-01T15:07:55Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop for stimulation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-for-stimulation/m-p/810925#M319808</link>
      <description>&lt;P&gt;The most important response has already been provided to you by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To get the best quality help, provide the best quality problem description - in this case a log of your program, which probably tells you exactly which line in your program has the array index out-of-order condition.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, I will take a guess.&amp;nbsp; You have defined the. array R of 100 elements, with 100 as the upper bound of the array index, and 1 as the lower-bound.&amp;nbsp; You have references to&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;R[I]&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;but I seems to always be an integer from 1 to 100 - i.e. within bounds.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So what about the reference&amp;nbsp; (with NOBS=100)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;        R[rand('gamma', &amp;amp;Nobs.,1)] + (diff&amp;gt;0);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I presume the rand('gamma',&amp;amp;NOBs.,1) returns a continuous result in the (0,100] interval - i.e. greater than zero and up through 100.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This means it may occasionally return a value less than 1.0.&amp;nbsp; &amp;nbsp;Let's say it returns a 0.8.&amp;nbsp; &amp;nbsp;But SAS will interpret the array reference R[0.8] as R[0] which doesn't exist.&amp;nbsp; &amp;nbsp;Probably some seeds generate a RAND result less than 1.0 earlier than others, generating the error message your refer to.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So be aware that SAS rounds down non-integer values to integers when indexing arrays.&amp;nbsp; Is that the behavior you want?&amp;nbsp; For instance, I'm sure you'll never populate the R[100] element, since you can't generate a number greater than 100.&amp;nbsp; Its probability is virtually zero.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edited additional note:&amp;nbsp; &amp;nbsp;You probably could change&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; R[rand('gamma', &amp;amp;Nobs.,1)] + (diff&amp;gt;0);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;to&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; R[1+rand('gamma', &amp;amp;Nobs.,1)] + (diff&amp;gt;0);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;which would effectively round UP non-integer RAND results.&amp;nbsp; This would likely eliminate the occasional out-of-range notes.&amp;nbsp; But I have no idea whether it will do the task that you intend, or whether it is consistent with your other references to the array.&lt;/P&gt;</description>
      <pubDate>Sun, 01 May 2022 15:09:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-for-stimulation/m-p/810925#M319808</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-05-01T15:09:57Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop for stimulation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-for-stimulation/m-p/810929#M319810</link>
      <description>&lt;P&gt;Thank you so much for your quick response.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to do fractional bootstrapping (&lt;A href="https://arxiv.org/pdf/1808.08199.pdf" target="_self"&gt;https://arxiv.org/pdf/1808.08199.pdf&lt;/A&gt;&amp;nbsp;) for rare outcomes. The number of resampling (or number of hits) is not an integer but continuous (we call weight) and the sum of these weights have to equal the sample size and variance = mean&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example, I want the expected weight.&lt;/P&gt;
&lt;P&gt;N&amp;nbsp; ID&amp;nbsp; expected weight&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; 0.2&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; 2.2&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; 0.6&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; 0.5&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; 0.8&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; 1.7&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Dunne_0-1651417923572.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/71055iA0B5BB7B441CEF45/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Dunne_0-1651417923572.png" alt="Dunne_0-1651417923572.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Dunne_1-1651417957834.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/71056i656F9F8354E2111C/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Dunne_1-1651417957834.png" alt="Dunne_1-1651417957834.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 01 May 2022 15:14:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-for-stimulation/m-p/810929#M319810</guid>
      <dc:creator>Dunne</dc:creator>
      <dc:date>2022-05-01T15:14:01Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop for stimulation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-for-stimulation/m-p/810930#M319811</link>
      <description>&lt;P&gt;What is line 66 supposed to be doing?&lt;/P&gt;</description>
      <pubDate>Sun, 01 May 2022 15:17:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-for-stimulation/m-p/810930#M319811</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-05-01T15:17:30Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop for stimulation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-for-stimulation/m-p/810932#M319813</link>
      <description>&lt;P&gt;Thanks for your detailed explanation. I want value&amp;nbsp; 0&amp;lt;"value"&amp;lt;100&lt;/P&gt;</description>
      <pubDate>Sun, 01 May 2022 15:20:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-for-stimulation/m-p/810932#M319813</guid>
      <dc:creator>Dunne</dc:creator>
      <dc:date>2022-05-01T15:20:38Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop for stimulation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-for-stimulation/m-p/810933#M319814</link>
      <description>&lt;P&gt;If I want to generate not integer numbers but continuous numbers&amp;gt;0, how should I modify the below? I did not familiar with simulation but tried to re-use someone's code online (so I don't really understand all the codes). Many thanks!&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;R[I]&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 01 May 2022 15:29:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-for-stimulation/m-p/810933#M319814</guid>
      <dc:creator>Dunne</dc:creator>
      <dc:date>2022-05-01T15:29:04Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop for stimulation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-for-stimulation/m-p/810935#M319816</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/245107"&gt;@Dunne&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks for your detailed explanation. I want value&amp;nbsp; 0&amp;lt;"value"&amp;lt;100&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;And isn't that exactly what you are already generating in your RAND('gamma',100,1) expression?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It appears that you want to use that function to randomly draw from 100 "buckets" indexed by the array.&amp;nbsp; Right now you have no bucket for RAND results less than 1,&amp;nbsp; &amp;nbsp;But you do have 100 buckets for RAND results for&amp;nbsp; 1&amp;lt;=RAND&amp;lt;=100.&amp;nbsp; But that last bucket (number 100) is mapped only to a RAND result of exactly 100, which I suspect has a "probability mass" of zero.&amp;nbsp; Its neighbor, bucket number 99, is mapped to RAND results from 99&amp;lt;=RAND&amp;lt;100, with a probability &amp;gt;0&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In effect the array reference is using the FLOOR function to convert a non-integer result to the integer below it to identify an array element.&amp;nbsp; Instead you could apply the CEIL (for ceiling) function against the RAND function, as in&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; R[CEIL(rand('gamma', &amp;amp;Nobs.,1))] + (diff&amp;gt;0);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This would map the RAND function to the array index as follows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE style="width: 40%;" border="1" width="40%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="50%"&gt;&lt;FONT size="5"&gt;&lt;STRONG&gt;RAND Results&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="50%"&gt;&lt;FONT size="5"&gt;&lt;STRONG&gt;Element number for R[RAND....]&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="50%" class="lia-align-center"&gt;&lt;STRONG&gt;0&amp;lt;RAND&amp;lt;=1&lt;/STRONG&gt;&lt;/TD&gt;
&lt;TD width="50%" class="lia-align-center"&gt;&lt;STRONG&gt;1&lt;/STRONG&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="50%" class="lia-align-center"&gt;&lt;STRONG&gt;1&amp;lt;RAND&amp;lt;=2&lt;/STRONG&gt;&lt;/TD&gt;
&lt;TD width="50%" class="lia-align-center"&gt;&lt;STRONG&gt;2&lt;/STRONG&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="50%" class="lia-align-center"&gt;&lt;STRONG&gt;...&lt;/STRONG&gt;&lt;/TD&gt;
&lt;TD width="50%" class="lia-align-center"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="50%" class="lia-align-center"&gt;&lt;STRONG&gt;...&lt;/STRONG&gt;&lt;/TD&gt;
&lt;TD width="50%" class="lia-align-center"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="50%" class="lia-align-center"&gt;&lt;STRONG&gt;98&amp;lt;RAND&amp;lt;=99&lt;/STRONG&gt;&lt;/TD&gt;
&lt;TD width="50%" class="lia-align-center"&gt;&lt;STRONG&gt;99&lt;/STRONG&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="50%" class="lia-align-center"&gt;&lt;STRONG&gt;99&amp;lt;RAND&amp;lt;=100&lt;/STRONG&gt;&lt;/TD&gt;
&lt;TD width="50%" class="lia-align-center"&gt;&lt;STRONG&gt;100&lt;/STRONG&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So you would now be randomly drawing from 100 ordered buckets of equal "size" using a gamma distribution.&lt;/P&gt;</description>
      <pubDate>Sun, 01 May 2022 16:02:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-for-stimulation/m-p/810935#M319816</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-05-01T16:02:29Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop for stimulation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-for-stimulation/m-p/810974#M319848</link>
      <description>&lt;P&gt;Thank you so much!&lt;BR /&gt;&amp;nbsp;It works now if I generate only 1 sample (by deleting line 22 , 42, 43 below). However, it failed to repeat the process &amp;amp;nboot. times (I want to generate &amp;amp;nboot. samples of the entire process). Could you please help me with this as well?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Dunne_0-1651443127462.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/71059iB42036AB425E0DCE/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Dunne_0-1651443127462.png" alt="Dunne_0-1651443127462.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 01 May 2022 22:14:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-for-stimulation/m-p/810974#M319848</guid>
      <dc:creator>Dunne</dc:creator>
      <dc:date>2022-05-01T22:14:20Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop for stimulation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-for-stimulation/m-p/810976#M319850</link>
      <description>&lt;P&gt;Instead of pasting an image of the log into your note, could you please capture the text and insert it as fixed width content (use the "&amp;lt;/&amp;gt;" icon to open a text box for pasting).&amp;nbsp; &amp;nbsp; I find it difficult to read the image.&lt;/P&gt;</description>
      <pubDate>Sun, 01 May 2022 22:22:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-for-stimulation/m-p/810976#M319850</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-05-01T22:22:52Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop for stimulation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-for-stimulation/m-p/810983#M319852</link>
      <description>&lt;PRE&gt;1                                                          The SAS System                                  18:27 Sunday, May 1, 2022

1          ;*';*";*/;quit;run;
2          OPTIONS PAGENO=MIN;
3          %LET _CLIENTTASKLABEL='Program';
4          %LET _CLIENTPROCESSFLOWNAME='Standalone Not In Project';
5          %LET _CLIENTPROJECTPATH='';
6          %LET _CLIENTPROJECTPATHHOST='';
7          %LET _CLIENTPROJECTNAME='';
8          %LET _SASPROGRAMFILE='';
9          %LET _SASPROGRAMFILEHOST='';
10         
11         ODS _ALL_ CLOSE;
12         OPTIONS DEV=SVG;
13         GOPTIONS XPIXELS=0 YPIXELS=0;
14         %macro HTML5AccessibleGraphSupported;
15             %if %_SAS_VERCOMP(9, 4, 4) &amp;gt;= 0 %then ACCESSIBLE_GRAPH;
16         %mend;
17         FILENAME EGHTML TEMP;
18         ODS HTML5(ID=EGHTML) FILE=EGHTML
19             OPTIONS(BITMAP_MODE='INLINE')
20             %HTML5AccessibleGraphSupported
21             ENCODING='utf-8'
22             STYLE=HtmlBlue
23             NOGTITLE
24             NOGFOOTNOTE
25             GPATH=&amp;amp;sasworklocation
26         ;
NOTE: Writing HTML5(EGHTML) Body file: EGHTML
27         
28         %let nobs=100;
29         %let nboot=200;
30         
31         data weight_1;
32         do i=1 to &amp;amp;nobs.;
33         output;
34         end;
35         run;

NOTE: The data set WORK.WEIGHT_1 has 100 observations and 1 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
      

36         
37         
38         data weight_2;
39         set weight_1 end=lastobs;
40         call streaminit (1293);
41         do n=1 to &amp;amp;nboot.;
42         
43         array R [&amp;amp;nobs.] _temporary_;
44         
45         if _N_=1 then do;
46         
47         do I=1 to (&amp;amp;nobs.);
48         
49         R(I)=rand('gamma',1,1);
50         
2                                                          The SAS System                                  18:27 Sunday, May 1, 2022

51         end;
52         
53         
54         diff=&amp;amp;nobs. - sum(of R[*]);
55         
56         do I=1 to diff;
57         
58         R[ceil(rand('gamma', &amp;amp;nobs.,1))] + (diff&amp;gt;0);
59         
60         end;
61         
62         VET+sum(of R[*]);
63         
64         end;
65         
66         ran=r[_N_];
67         output;
68         end;
69         
70         run;

ERROR: Array subscript out of range at line 58 column 1.
lastobs=0 i=1 n=2 diff=4.7355896866 VET=105.65851235 ran=0.3109976073 _ERROR_=1 _N_=1
NOTE: The SAS System stopped processing this step because of errors.
NOTE: There were 2 observations read from the data set WORK.WEIGHT_1.
WARNING: The data set WORK.WEIGHT_2 may be incomplete.  When this step was stopped there were 1 observations and 5 variables.
WARNING: Data set WORK.WEIGHT_2 was not replaced because this step was stopped.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
      

71         
72         %LET _CLIENTTASKLABEL=;
73         %LET _CLIENTPROCESSFLOWNAME=;
74         %LET _CLIENTPROJECTPATH=;
75         %LET _CLIENTPROJECTPATHHOST=;
76         %LET _CLIENTPROJECTNAME=;
77         %LET _SASPROGRAMFILE=;
78         %LET _SASPROGRAMFILEHOST=;
79         
80         ;*';*";*/;quit;run;
81         ODS _ALL_ CLOSE;
82         
83         
84         QUIT; RUN;
85         
&lt;/PRE&gt;
&lt;P&gt;If I remove line 41, 67, 68, the code will work. But I want to replicate the code &amp;amp;nboot. times. Should I delete "call streaminit(1293)" to have different seeds for each sample?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sorry for my slow response because I want to check my code carefully.&lt;/P&gt;</description>
      <pubDate>Sun, 01 May 2022 22:52:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-for-stimulation/m-p/810983#M319852</guid>
      <dc:creator>Dunne</dc:creator>
      <dc:date>2022-05-01T22:52:34Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop for stimulation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-for-stimulation/m-p/810988#M319855</link>
      <description>&lt;P&gt;First, thank you for posting the code in a text box - MUCH easier on my eyes - and it preserves the fixed pitch font in the SAS log - which is occasionally important.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have checked on my previous comment&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;I presume the rand('gamma',&amp;amp;NOBs.,1) returns a continuous result in the (0,100] interval - i.e. greater than zero and up through 100.&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I should have done this before, because the &lt;A href="https://support.sas.com/documentation/cdl/en/lefunctionsref/63354/HTML/default/viewer.htm#p170xcnh05pr8gn1vtvi77x35ltt.htm" target="_self"&gt;SAS Functions and Call Routines&lt;/A&gt;&amp;nbsp;documentation for the GAMMA distribution just says the the RAND('GAMMA'... function will produce a positive number.&amp;nbsp; There is no upper limit, so you may very well be generating numbers greater than 100, which could cause the error message you report.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So if you have a strategy of a fixed number (100 in your case) of buckets selected based on the RAND('GAMMA' function, you will have to decide whether 100 buckets (i.e. macrovar NOBS=100) is enough, and what to do about results from RAND('GAMMA' that exceed &amp;amp;NOBS.&amp;nbsp; &amp;nbsp;You could replace the single statement&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;        R[rand('gamma', &amp;amp;Nobs.,1)] + (diff&amp;gt;0);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;with&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;        J=ceil(rand('gamma', &amp;amp;Nobs.,1));
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and then decide whether to add&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;        R[J] + (diff&amp;gt;0);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;depending on the value of J.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Of course, it's not at all clear what your goal is, so you may need to take another strategy.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 02 May 2022 03:11:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-for-stimulation/m-p/810988#M319855</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-05-02T03:11:57Z</dc:date>
    </item>
  </channel>
</rss>

