I have a data of 5 clusters, each has 30 observations. I am asked to create a new variable, which receives the value of x with probability of 0.5; and -x with probability of 0.5 assigned at cluster level.
My code is:
Code:
bysort groupid: gen clus_prob= rnormal(0,1) bysort groupid: replace clus_prop=clus_prop[1] egen max_prop=max(clus_prop) gen newvar=x replace newvar= -x if clus_prop>=max_prop/2
Can you please help?
Thank you so much.
Lanna
0 Response to Assign value randomly
Post a Comment