I have a dataset containing stock data, looking like this:


Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input str10 A str45 B str17 C str29 D str23 E str28 F str45 G str14 H
" "          "ERROR"                                         "ADEVINTA"     "ADEVINTA - TURNOVER BY VOLUME" "ADEVINTA - MARKET VALUE" "ADEVINTA - PRICE TO BOOK VAL" "ERROR"                                         "AF GRUPPEN 'A'"
"Code"       ""                                              "N:ADEB(P)"    "N:ADEB(VO)"                    "N:ADEB(MV)"              "N:ADEB(PTBV)"                 ""                                              "N:AFG(P)"      
"ISIN"       "NO0010844038"                                  "NO0010844038" "NO0010844038"                  "NO0010844038"            "NO0010844038"                 "NO0003078107"                                  "NO0003078107"  
" 6/30/2017" "$$ER: E100,INVALID CODE OR EXPRESSION ENTERED" "NA"           "NA"                            "NA"                      "NA"                           "$$ER: E100,INVALID CODE OR EXPRESSION ENTERED" "150"           
"  7/3/2017" ""                                              "NA"           "NA"                            "NA"                      "NA"                           ""                                              "150"           
"  7/4/2017" ""                                              "NA"           "NA"                            "NA"                      "NA"                           ""                                              "149.5"         
end

In order to reshape the data as done by David Benson in this post: https://www.statalist.org/forums/for...gaCLup2Du7ebZY
I first need to add prefixes to the different ISIN-codes (to ensure unique variable names), and then make these the variable names.

I am struggling with creating a loop that e.g. starts at the 3rd column, and for every 5th column adds "n_" to each observation in line/row 3.

I am able to do this to already defined variable names, but i need to rename the data in row 3 before i define it as variable names, in order to ensure unique variable names.

I have tried writing the following code:

Code:
d, s
return list
unab varlist : *
tokenize "`varlist'"

forval j = 3(5)`r(k)'{
    rename ``j'' p_``j'[3]'
}
but it gives me the error "variable p_ already defined r(110)".

I have tried not tokenizing "varlist", using only:

Code:
forval j = 3(5)`r(k)' {
    rename `j' ``j'[3]'
}
but then i get "invalid syntax r(198)", and i am not able to figure out what I do wrong.


Does anybody know how to make this work?

Best regards,
Axel