Skip to content

Instantly share code, notes, and snippets.

@onesandzeroes
Created August 4, 2014 23:23
Show Gist options
  • Save onesandzeroes/0c5ed87648bb94a9af3b to your computer and use it in GitHub Desktop.
Save onesandzeroes/0c5ed87648bb94a9af3b to your computer and use it in GitHub Desktop.
Blocked random assignment
program assign_blocks
version 13.1
syntax [varlist], totaln(integer) blocksize(integer)
drop _all
set obs `totaln'
generate treatment_assigned = 1
generate dice_roll = runiform()
generate block = 1
local half_block = `blocksize' / 2
local n_blocks = `totaln' / `blocksize'
forvalues block_number = 1/`n_blocks' {
local blockstart = ((`block_number' - 1) * `blocksize') + 1
local blockend = ((`block_number' - 1) * `blocksize') + `blocksize'
replace block = `block_number' in `blockstart'/`blockend'
create_block, na(`half_block') nb(`half_block') startindex(`blockstart') endindex(`blockend')
}
end
* final line
program create_block
version 13.1
syntax [varlist], na(integer) nb(integer) startindex(integer) endindex(integer)
local total_N = `na' + `nb'
local n1 = 0
local n2 = 0
forvalues row_number = `startindex'/`endindex' {
local p1 = (`na' - `n1') / (`na' + `nb' - `n1' - `n2')
local current_roll = dice_roll in `row_number'
local is_two = `p1' < dice_roll in `row_number'
replace treatment_assigned = 2 if `is_two' in `row_number'
count if treatment_assigned == 1 in `startindex'/`row_number'
local n1 = r(N)
count if treatment_assigned == 2 in `startindex'/`row_number'
local n2 = r(N)
}
end
* Final line must contain newline
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment