Hi,
I'm currently working on a dataset containing stock ( and other) prices in a firm's home currency, and want to convert these to USD so that the values can be compared.
fic => identifies countries, looks like NLD, SWE, GER etc
prccd => stockprice variable
ex##us => exchange rates, number of foreign currency per USD. looks like exeuus, exsdus, etc.
while I could use the following for every variable to be replaced,
it would require a lot of editing and allow for errors when I am adjusting all the variables . Therefore, I wanted to make a loop. I currently have the following.
The IF-function above is triggered as both fic and the substring are in string format. However, I'm trying to solve the problem where I currently have RUNASVAR()
The substring there returns "exsdus". the idea is that the function returns the value stored inside the variable exsdus (which would be exchange rate 7.40344 or something) and not just the name.
When I used to code in mIRC, I would have utilized the following code:
As in that programming language % identifies a variable, and I could use brackets just like you use them in math to let mIRC compute whatever is inside the brackets first, and then run it as variable by removing the spaces afterwards through the use of $+
Could anyone tell me if there is something in stata that would allow me to get the job done?
EDIT: If it helps to find a potentially different solution, all exchange rate variables start with ex* -- However, I haven't been able to come up with a foreach-function solution that may work.
PS: Given that I am working with a 20-year dataset, at some point some currency pairs need to be changed (e.g. eurozone countries), but that is something I'll solve easily.
I'm currently working on a dataset containing stock ( and other) prices in a firm's home currency, and want to convert these to USD so that the values can be compared.
fic => identifies countries, looks like NLD, SWE, GER etc
prccd => stockprice variable
ex##us => exchange rates, number of foreign currency per USD. looks like exeuus, exsdus, etc.
while I could use the following for every variable to be replaced,
Code:
gen USDmcap = . gen USDstockprice = . gen USDtotalrepo = . //etc replace USDstockprice = prccd / exsdus if fic == "SWE" replace USDmcap = marketcap / exsdus if fic == "SWE" //etc
Code:
g str9 matchficex = "a" replace matchficex = "SWEexsdus" if fic == "SWE" // ======= manually copy+paste the above 30 times and only once adjust it so that the proper combination of country identifiers + currency vars are matched // ... //replace USDstockprice = prccd / RUNASVAR(substr(matchficex,4,9)) if fic == substr(matchficex,1,3)
The substring there returns "exsdus". the idea is that the function returns the value stored inside the variable exsdus (which would be exchange rate 7.40344 or something) and not just the name.
When I used to code in mIRC, I would have utilized the following code:
Code:
//replace %USDstockprice = %prccd / %ex [ $+ [ substr(matchficex,6,9) ] ] if %fic == substr(matchficex,1,3) or //replace %USDstockprice = %prccd / % [ $+ [ substr(matchficex,4,9) ] ] if %fic == substr(matchficex,1,3)
Could anyone tell me if there is something in stata that would allow me to get the job done?
EDIT: If it helps to find a potentially different solution, all exchange rate variables start with ex* -- However, I haven't been able to come up with a foreach-function solution that may work.
PS: Given that I am working with a 20-year dataset, at some point some currency pairs need to be changed (e.g. eurozone countries), but that is something I'll solve easily.
Comment