Dear all,
Consider the following data:
I want to generate a series of t variables for all values of index variable. Take values 1010 and 1011 of index variable as an example:
The code above works but I have to repeat and revise the code many time. Is there a shorter way to obtain what I want? Thank you.
Consider the following data:
clear
input int index byte age
1010 26
1011 41
1020 28
1021 46
1030 32
1031 44
1040 27
1041 28
2010 49
2011 45
2020 25
2021 29
2030 25
2031 29
2040 49
2041 48
3010 28
3011 48
3020 43
3021 28
3030 43
3031 48
3040 35
3041 48
4010 36
4011 37
4020 40
4021 45
4030 30
4031 49
4040 49
4041 33
5010 40
5011 43
5020 38
5021 46
5030 49
5031 27
5040 46
5041 49
6010 24
6011 23
6020 36
6021 49
6030 35
6031 47
6040 37
6041 46
7010 43
7011 27
7020 48
7021 30
7030 47
7031 39
7040 43
7041 28
8010 40
8011 40
8020 30
8021 45
8030 45
8031 30
8040 35
8041 38
9010 38
9011 44
9020 44
9021 49
9030 45
9031 32
9040 28
9041 44
10101 31
10110 38
10111 44
10120 37
10121 35
10130 20
10131 39
10140 40
10141 29
10210 36
10211 49
10220 25
10221 32
10230 15
10231 42
10240 35
10241 22
end
input int index byte age
1010 26
1011 41
1020 28
1021 46
1030 32
1031 44
1040 27
1041 28
2010 49
2011 45
2020 25
2021 29
2030 25
2031 29
2040 49
2041 48
3010 28
3011 48
3020 43
3021 28
3030 43
3031 48
3040 35
3041 48
4010 36
4011 37
4020 40
4021 45
4030 30
4031 49
4040 49
4041 33
5010 40
5011 43
5020 38
5021 46
5030 49
5031 27
5040 46
5041 49
6010 24
6011 23
6020 36
6021 49
6030 35
6031 47
6040 37
6041 46
7010 43
7011 27
7020 48
7021 30
7030 47
7031 39
7040 43
7041 28
8010 40
8011 40
8020 30
8021 45
8030 45
8031 30
8040 35
8041 38
9010 38
9011 44
9020 44
9021 49
9030 45
9031 32
9040 28
9041 44
10101 31
10110 38
10111 44
10120 37
10121 35
10130 20
10131 39
10140 40
10141 29
10210 36
10211 49
10220 25
10221 32
10230 15
10231 42
10240 35
10241 22
end
Code:
xi i.index
forval i = 1010/1011 {
cap gen double t`i' = 0
cap replace t`i' = (49 - age) if _Iindex_`i'==1
}

Comment