Dear stata users,

I want to do a couple of edits to 35 variables, I have a dataset containing 35 columns with codes of injuries for 600 patients(with data in rows). These injury codes have meaning as they belong to different body regions, and that's where I am interested at. I want to give an overview in my paper of the number and percentage of patients that have an injury in a certain body region. I already generated that variable. The thing I can't get working is making stata replace the x by the number of the loop(starting with 1) and thereby reading the inj_x_ais_score as inj_1_ais_score and recognizing thereby the variable in the set and thereby get the thing running. The dumb solution to this would be to copy this code 35 times and replace the x manually from 1 to 35 but I hope that's not necessary.

My code looks like this:

forval x = 1/35{

drop inj_x_ais_score

drop inj_ais_string_x

tostring inj_ais_x, generate(inj_ais_string_x)
order inj_ais_string_x, after(inj_ais_x)
generate inj_x_ais_score = substr(inj_ais_string_x,8,1)
order inj_x_ais_score, after(inj_x_body_region)
destring inj_x_ais_score, replace
replace inj_x_body_region = 1 if inj_ais_x >= 100000 & inj_ais_x <= 200000
replace inj_x_body_region = 2 if inj_ais_x >= 200000 & inj_ais_x <= 300000
replace inj_x_body_region = 3 if inj_ais_x >= 300000 & inj_ais_x <= 400000
replace inj_x_body_region = 4 if inj_ais_x >= 400000 & inj_ais_x <= 500000
replace inj_x_body_region = 5 if inj_ais_x >= 500000 & inj_ais_x <= 600000
replace inj_x_body_region = 6 if inj_ais_x >= 600000 & inj_ais_x <= 700000
replace inj_x_body_region = 7 if inj_ais_x >= 700000 & inj_ais_x <= 900000
replace inj_x_body_region = 9 if inj_ais_x >= 900000 & inj_ais_x <= 1000000
}

This happens:

. forval x = 1/35{
2.
.
. tostring inj_ais_x, generate(inj_ais_string_x)
3. order inj_ais_string_x, after(inj_ais_x)
4. generate inj_x_ais_score = substr(inj_ais_string_x,8,1)
5. order inj_x_ais_score, after(inj_x_body_region)
6. destring inj_x_ais_score, replace
7. replace inj_x_body_region = 1 if inj_ais_x >= 100000 & inj_ais_x <= 200000
8. replace inj_x_body_region = 2 if inj_ais_x >= 200000 & inj_ais_x <= 300000
9. replace inj_x_body_region = 3 if inj_ais_x >= 300000 & inj_ais_x <= 400000
10. replace inj_x_body_region = 4 if inj_ais_x >= 400000 & inj_ais_x <= 500000
11. replace inj_x_body_region = 5 if inj_ais_x >= 500000 & inj_ais_x <= 600000
12. replace inj_x_body_region = 6 if inj_ais_x >= 600000 & inj_ais_x <= 700000
13. replace inj_x_body_region = 7 if inj_ais_x >= 700000 & inj_ais_x <= 900000
14. replace inj_x_body_region = 9 if inj_ais_x >= 900000 & inj_ais_x <= 1000000
15. }
variable inj_ais_x not found
r(111);

end of do-file


Can anyone help me out? that would be great, also to be a little bit more time efficiënt in the future

Best regards,

Anne Fokkema(surgery resident conducting research after thoracic trauma)