Hello,
The problem arises here:
"foreach w in ca cb cc cd ce cf ck cg ch ci {
// find the wave number
local waveno=strpos("abcdefghijklmnopqrstuvwxyz","`w'")"
When going through the subsequent iterations of the loop, Stata replace `w' with ca (1st iteration), cb (2nd iteration), cc (3rd iteration) and so on, instead of a, b, c, ..., and this can't produce a meaningful result. The strpos("abcdefghijklmnopqrstuvwxyz", "`w'") returns the position of the characters you specify in the `w' part in the string "abcdefghijklmnopqrstuvwxyz" counting from the left. When the loop is correctly specified, in the 1st iteration Stata replaces "`w'" with "a", then strpos returns the value of 1 which saved in the local macro waveno, in the 2nd iteration Stata replaces the "`w'" with "b", then strpos returns the value of 2 again saved in the local macro waveno and so on. The value of the local waveno is then passed to the variable wave created in line 25 (gen wave=`waveno'), in 1st iteration wave=1, in the 2nd wave=2 and so on.
I've corrected the file (attached), I'm also attaching the comparison of your original file with all the corrections marked so you can see what changes and where were necessary (your original file is on the right).
Best wishes,
Understanding Society User Support Team