Hello,
my dataset is structered the following:
clear
input float (group_id won_close_dummy loss_close_dummy)
1 0 1
2 1 0
2 1 0
3 1 0
6 1 0
7 0 1
9 0 0
13 0 1
13 1 0
13 0 1
13 0 1
17 1 0
17 0 1
18 0 1
20 1 0
21 1 0
24 1 0
24 1 0
25 0 1
28 0 1
33 1 0
34 1 0
35 1 0
38 0 0
42 1 0
43 1 0
43 1 0
43 1 0
43 1 0
43 1 0
45 1 0
45 0 1
46 0 1
46 1 0
47 1 0
47 0 1
I now want to create a new binary variable "donated" which takes the value of 0 if within a group there is at least one 1 for both "won_close_dummy" and "loss_close_dummy". This means if within a group (eg. group_id ==13 or group_id==17) both variables "won_close_dummy" and "loss_close_dummy" have at least one 1 the new variable "donated" should get a 0 for the whole group.
If within a group (eg. group_id==2 or group_id==43) only one variable has a 1 the new variable "donated" should get a 1 for the hole group.
This means it should look like this in the end:
input float(group_id won_close_dummy loss_close_dummy donated)
1 0 1 1
2 1 0 1
2 1 0 1
3 1 0 1
6 1 0 1
7 0 1 1
9 0 0 1
13 0 1 0
13 1 0 0
13 0 1 0
13 0 1 0
17 1 0 0
17 0 1 0
18 0 1 1
20 1 0 1
21 1 0 1
24 1 0 1
24 1 0 1
25 0 1 1
28 0 1 1
33 1 0 1
34 1 0 1
35 1 0 1
38 0 0 1
42 1 0 1
43 1 0 1
43 1 0 1
43 1 0 1
43 1 0 1
43 1 0 1
45 1 0 0
45 0 1 0
46 0 1 0
46 1 0 0
47 1 0 0
47 0 1 0
I hope you can help me solving this problem. Thanks in advance!
Greetings
Marcel
Related Posts with Create a new variable based on observations within a group
Regress in fixed time periodI have a dataset from 2004-2022 and want to form a regression every rolling 6 months so jan 2004-jun…
Create new variable using coefficient from a variable containing time dummyHello everyone, I'm using Stata version 14.0. I'm actually trying to estimate the natural rate of un…
Multiply obverations of a variable with a vector to creat a new data setHi experts! I am a beginner of Stata and looking for your help. I have data set 1 or matrix 1 (63x1)…
Looping identify autosomal monosomies | regexmHello all: I am trying through loop through a string of karyotype to identify occurrence of "-1" th…
About clogit in interventional studyHello. I would like to ask your help about clogit. Interventional study was conducted, the data was …
Subscribe to:
Post Comments (Atom)
0 Response to Create a new variable based on observations within a group
Post a Comment