How to randomly assign strings from a macro to observations

bill999

I have 4000 observations, 1000 observations each for 4 different months. I have a global macro, treatment, that I want to randomly assign to my observations. I want to separately do this for each month. treatment takes on 35 values. For each month, I want the first 20 values of treatment to each have 29 observations and to have the last 15 values of treatment to each have 28 observations each. That makes 1000 total/month. For example, the first 1000 observations are Jan. I want A01 to be randomly assigned to 29 observations within Jan. ... And G05 to be randomly assigned to 28 observations of Jan.

clear
set obs 4000
gen ID = _n
gen month = "Jan" if _n<=1000
replace month = "Feb" if _n>=1001 & _n<=2000
replace month = "Mar if _n>=2001 & _n<=3000
replace month = "Apr" if _n>=3001 & _n<=4000


*Create treatment
global letters A B C D E F G
global numbers 01 02 03 04 05
global treatment ""
foreach i in $letters {
    foreach j in $numbers {
            global treatment $treatment `i'`j'
    }
}

At this point, what is the best way to randomly assign treatment to observations - separately for each month?

The way that I thought of would be to create a runiform() variable; sort by this variable and also by month; then go about allocating the first value of treatment to the first 29 observations and so on. But I am hoping there is a better way than this.

Roberto Ferrer

That's probably how I'd do it:

clear
set more off

*----- example data -----

set obs 2000
gen month = cond(_n <= 1000, 1, 2)

*----- what you want -----

bysort month: gen orig = _n

set seed 3596
gen runi = runiform()

bysort month (runi): egen treat1 = seq() if orig <= 580, from(1) to(20)
by month: egen treat2 = seq() if orig >= 581, from(21) to(35)

gen treat = max(treat1, treat2)

sort orig
drop treat?
tab treat month

Then all you need to do is map the integers 1-35 to treatments which you can do with value labels. I've used numbers in my example, but it is easy to make it more general.

There are other ways of creating the sequences, which you can check in the Stata FAQ pages.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to randomly assign strings from a macro to observations

From Dev

How to randomly assign variables

From Dev

Randomly Assign Observations into either Test or Control for a SAS dataset

From Dev

How to extract unique strings from a macro?

From Dev

How to assign matrix elements randomly

From Dev

How to assign matrix elements randomly

From Dev

How to randomly get a string from a collection but prefer strings at the beginning of the collection

From Dev

How to randomly pick a string from multiple finals strings in Java?

From Dev

How to randomly select a string between two strings from ENUM in java?

From Dev

How to display randomly selected strings from an array in java without repetitions

From Dev

How can I randomly select from prewritten strings in C#?

From Dev

Randomly Assign Records From One Table to Another

From Dev

Randomly select from array then assign selection to a variable

From Dev

How to assign specific colors to an object randomly?

From Dev

How to randomly assign value to textbox onload

From Dev

How to randomly assign button content and colour?

From Dev

How to assign start times of observations to blocks of hours in R

From Dev

How to randomly choose one of several strings?

From Dev

How to use variadic macro to assign function pointer

From Dev

How to assign a MS Word macro to a hotkey?

From Dev

oooBasic : how to assign a key to a macro, programmatically?

From Dev

How to assign macro to a table column in Excel?

From Dev

VBA Excel 2010 - How to assign a macro to a command button that was made with a macro

From Dev

Copy data randomly from one file and paste it randomly in other Excel using macro

From Dev

Create random values from sequence but with exact unique number of observations repeated randomly

From Dev

how can i assign a randomly generated integer to a string in C?

From Dev

How to randomly assign values row-wise in a numpy array

From Dev

How to assign Randomly generated values inside of multiple object

From Dev

How to randomly assign each observation to one of two labels

Related Related

  1. 1

    How to randomly assign strings from a macro to observations

  2. 2

    How to randomly assign variables

  3. 3

    Randomly Assign Observations into either Test or Control for a SAS dataset

  4. 4

    How to extract unique strings from a macro?

  5. 5

    How to assign matrix elements randomly

  6. 6

    How to assign matrix elements randomly

  7. 7

    How to randomly get a string from a collection but prefer strings at the beginning of the collection

  8. 8

    How to randomly pick a string from multiple finals strings in Java?

  9. 9

    How to randomly select a string between two strings from ENUM in java?

  10. 10

    How to display randomly selected strings from an array in java without repetitions

  11. 11

    How can I randomly select from prewritten strings in C#?

  12. 12

    Randomly Assign Records From One Table to Another

  13. 13

    Randomly select from array then assign selection to a variable

  14. 14

    How to assign specific colors to an object randomly?

  15. 15

    How to randomly assign value to textbox onload

  16. 16

    How to randomly assign button content and colour?

  17. 17

    How to assign start times of observations to blocks of hours in R

  18. 18

    How to randomly choose one of several strings?

  19. 19

    How to use variadic macro to assign function pointer

  20. 20

    How to assign a MS Word macro to a hotkey?

  21. 21

    oooBasic : how to assign a key to a macro, programmatically?

  22. 22

    How to assign macro to a table column in Excel?

  23. 23

    VBA Excel 2010 - How to assign a macro to a command button that was made with a macro

  24. 24

    Copy data randomly from one file and paste it randomly in other Excel using macro

  25. 25

    Create random values from sequence but with exact unique number of observations repeated randomly

  26. 26

    how can i assign a randomly generated integer to a string in C?

  27. 27

    How to randomly assign values row-wise in a numpy array

  28. 28

    How to assign Randomly generated values inside of multiple object

  29. 29

    How to randomly assign each observation to one of two labels

HotTag

Archive