*! version 0.5.0 2013-10-06

//  tpr - VanHeuvelen & Long (tvanheuv@indiana.edu)
//  create sampling variables

program define tpr_pop_sampling

    version 13
    tempvar u1 u2
    qui {
        gen double `u1' = .
        gen double `u2' = .
        local sample_maxdrawn = $sample_maxdrawn
        local samplesizes $sample_smallest $sample_sizes
        foreach sampn of numlist `samplesizes' {
            nois di _new "Generating samples of size `sampn': "
            if (`sampn'==50) global sample_maxdrawn ///
                = `sample_maxdrawn'*$sample_50factor
            else if (`sampn'==100) global sample_maxdrawn ///
                = `sample_maxdrawn'*$sample_100factor
            else global sample_maxdrawn = `sample_maxdrawn'
            forvalues isamp = 1/$sample_maxdrawn {
                replace `u1' = runiform()
                replace `u2' = runiform()
                sort `u1' `u2' // see Stata blog for rationale
                gen byte s`sampn'i`isamp' = _n<=`sampn'
                label var  s`sampn'i`isamp' "n=`sampn' #`isamp'"
                local dotit = mod(`isamp',100)==0
                nois if `dotit' noi di _continue ".`isamp'"
            }
        }
    }

end
exit