Hi,

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
However, I think it is not what I am asked to do. Another intuition is that I create a var randomly receive values [1;-1]. However, I do not know how to do it.
Can you please help?
Thank you so much.

Lanna