Quantcast
Channel: Statalist
Viewing all articles
Browse latest Browse all 72762

Can someone help convert this SAS simulation code to Stata?

$
0
0
Hi everyone,

I have a SAS code for simulating the effect size and standard error for a meta-analytic data-set of K studies over L iterations. Would be very grateful if someone could help convert this to Stata code or point me to some automated way of doing so.
Many thanks
Suhail Doi

Code:
%let OR = 2;
%let phi2 = 0;
%let K = 10;
%let L=1000;
*    Create a set of data with K points;
data output;
/*Start loop*/
do r=1 to &L;
     do idx = 1 to &K. ;
 
*    Step a;
           theta = log(&OR.);
*    Step b;
/*No Delaporte distribution implemented in SAS, using Native Binomial distribution instead*/
           ub = rand("Uniform");
           bl=100;
           bh=1000;
           n = bl + (bh-bl)*ub;
 
*    Step c;
 
           uc = rand("Uniform");
           cl=0.82;
           ch=0.993;
           p_nc = cl + (ch-cl)*uc;
 
 
    
*    Step d;
 
           ud = RAND('UNIFORM');
           dl=0.454;
           dh=0.545;
           tmpd=dl + (dh-dl)*ud;
           b = tmpd*n* p_nc;
           d = n* p_nc-b;
 
*    Step e;
 
           ue = RAND('UNIFORM');
           el=0.1;
           eh=0.55;
 
           tmpe=el + (eh-el)*ue;
           a=tmpe*n*(1 - p_nc);
           c = n * (1 - p_nc) - a;
 
*    Step f;
           if mod(idx,2)=1 then do;  
                p_2 = RAND('BETA', b, d);
                p_1 = (p_2 * exp(theta)) / (1 - p_2 + (p_2 * exp(theta)));
    
           end;
           if mod(idx,2)=0 then do;
                p_1 = RAND('BETA', a, c);
                p_2 = (p_1 / exp(theta)) / (1 - p_1 + (p_1 / exp(theta)));
           end;
 
*    Step g;
           a_s = p_1 * n * (1 - p_nc);
           c_s = (n * (1 - p_nc)) - a_s;
           b_s = p_2 * n *p_nc;
           d_s = (n * p_nc) - b_s;
 
*    Step h;
           p_1s = RAND('BETA', a_s, c_s);
           p_2s = RAND('BETA', b_s, d_s);
*    Step i;
           theta_d = log((p_1s / (1 - p_1s))/(p_2s / (1 - p_2s)));
 
*    Step j;
 
          uj = RAND('UNIFORM');
          jl=1;
           jh=9;
           q=(jl + (jh-jl)*uj)/10;
*    Step k;
           phi2 = 0.000000000000001 + (&phi2. * (1 - q) / q);
 
*    Step l;
           theta_hat = RAND('NORMAL', theta_d, phi2);
 
*    Step m;
           p_1ss = (p_2 * exp(theta_hat)) / (1 - p_2 + (p_2 * exp(theta_hat)));
*    Step n - repeat step g with p_1ss and p_2;
           a_ss = p_1ss * n * (1 - p_nc);
           c_ss = n * (1 - p_nc) - a_ss;
 
*    Step o;
           if a_ss gt 0 and c_ss gt 0 and b_s gt 0 and d_s gt 0 then do;
           var_theta_hat = 1 / a_ss + 1 / c_ss + 1 / b_s + 1 / d_s;
               se=sqrt(var_theta_hat);
           end;
           output;
     end;
end;
run;

Viewing all articles
Browse latest Browse all 72762

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>