for the dataset like the following, I want to change the value of G7_A6 which is either 1 or 2 based on B1_04. However, for a given ID (there are two same IDs), G7_A6 takes either only 1 or only 2. I need to use loops in STATA because I have a very large dataset and typing individual IDs is cumbersome.
ID name B1_04 G7_A6
1 Sam 2 1
1 Margaret 1 2
9 Jim 5 1
9 Cinderella 1 1
29 George 2 2
29 Natalie 1 1
55 Michael 3 1
55 Cari 1 1
The commands are
replace G7_A6="2" if B1_04=="3" | B1_04 =="4" | B1_04 == "5" | B1_04 =="6" |B1_04 =="7" replace G7_A6="1" if B1_04=="2" with this command both the obersevations for ID 1 should have G7_A6 = 1 for ID 9 should have G7_A6 = 2 for ID 29 should have G7_A6 = 1 for ID 55 should have G7_A6 = 2 and so on Yours sincerely, Pankaj