Do loop - random number is generating the same number twice and isn't adding it in a running total

做~自己de王妃 提交于 2019-12-25 09:18:38

问题


Hey guys thank you so much, in advance, for your help! I have a data step before the code below called "simulation_tracking3", that outputs something like:

CDFx Allowed_Claims
.06   120
.12   13
.15   1400

My hash table averages the Allowed_Claims based on a randomly generated value (from 0 to 1). For example, let's call this Process A, if Px = rand('Uniform',0,1) yields .09, I want it to average between the Allowed_Claims values where Px = .06 and Px = 0.12, which is (120+13)/2

The role of the array is that it dictates how many iterations of Process A I want. The array is

Members {24} _temporary_ (5 6 8 10 12 15 20 25 30 40 50 60 70 80 
                                    90 100 125 150 175 200 250 300 400 500);

So when the loop starts, it will perform 5 iterations of Process A, thereby producing 5 averaged "allowed_claims" values. I want the sum of these five claims.

Then, the loop will continue and perform 6 iterations of Process A and produce 6 averaged "allowed_claims" values. Again, I want the sum of these 6 claims.

The output table looks like this:

`

Member[i]    Average_Expected_Claims
   5              (sum of 5 'averaged 'claims) 
   6              (sum of 6 'averaged' claims) 
   8              (sum of 8 'averaged' claims) 

My problem is that, on the second-to-last and last iteration, it's using the same random value to generate the allowed claims. and it doesn't sum it up in a "running total" format.

For example, the fourth and fifth iteration for the 5 member group have the same allowed claims, the fifth and sixth for the 6 member group, etc. Also, the number that is produced in the output table corresponding to a X-member group, sums the allowed claims value from the first occurrence to the second to last. For example, for the 5 person group the sum of the allowed claims is from 1-4, not 1-5.

So the two issues I see are: it doesn't generate a unique random value to calculate allowed claims; it fails to add the last occurrence to the running total.

The values for the 5-member generated table looks like this:

allowed_claims  _i_simul  rand_value    ac_average
805.61154253      1       0.4518515905  805.61154253
805.61154253      2       0.2017115643  0
5091.2264605      3       0.7019698818  4285.614918
8207.1931206      4       0.6518877812  3115.9666601
8207.1931206      5       0.6518877812  3115.9666601

Below is my code:

data simulation_members; *simulates allowed claims for each member in member array;  
    call streaminit(454); 
        array members [24] _temporary_ (5 6 8 10 12 15 20 25 30 40 50 
                                        60 70 80 90 100 125 150 175 200 250 300 400 500);  *any number of members here is fine;
if _n_ eq 1 then do; * initiliaze the hash tables;
if 0 then set simulation_tracking3; * defines the variables used; 
    declare hash _iter(dataset:'simulation_tracking3', ordered: 'a'); *ordered = ascending - do not need a sort first; 
        _iter.defineKey('CDFx');                    * key is artificial, but has to exist;
        _iter.defineData('CDFx','Allowed_Claims');  * data variables to retrieve;
        _iter.defineDone();
        declare hiter hi_iter('_iter');            * the iterator object;
end;


    do _i_member = 1 to dim(members);            * iterate over members array;
        call missing(claims_simulated);
            do _i_simul = 1 to members[_i_member]-1;
                rand_value = rand('Uniform',0,1);
                do rc = hi_iter.first() by 0 until (hi_iter.next() ne 0 or CDFx gt rand_value);
end;
                ac_max = allowed_claims; 
                rc = hi_iter.prev();
                ac_min = allowed_claims;
                ac_average = mean(ac_max,ac_min);
                claims_simulated + ac_average;
             put rand_value= claims_simulated=;                 *just for logging;
             output; 
         end; 
    putlog; 
output; *drop unnecessary columns; 
end; 
stop;
run;

回答1:


This has nothing to do with RNG and everything to do with programming logic. It's not generating a number twice, you're outputting the same row twice.

Remove the output that has the *drop unnecessary columns; comment, and update the loop to remove the -1.



来源:https://stackoverflow.com/questions/44500476/do-loop-random-number-is-generating-the-same-number-twice-and-isnt-adding-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!