Page 1 of 1

Replacing string characters

Posted: Thu Mar 10, 2022 3:07 pm
by Wiz_Ven
Dear friends, I am writing to ask you for help in solving the following problem. I have the following string representing my variables

e = "HPG = C(1) + C(2)*EPUTI + C(3)*INCGM + C(4)*LMSIM + C(5)*HPGM(-1) + C(7)*LMSIM(-1) + C(8)*INCGM(-1)"

that I would like to transform as follows

e = "HPG c EPUTI INCGM LMSIM EPUTI*HPGM(-1) EPUTI*INCGM EPUTI*INCGM(-1) EPUTI*LMSM EPUTI*LMSIM(-1)".

It is important that the variables appear in the exact order above. I used a combination of "@replace" commands but was unable to get the desired result. I hope some of you can kindly help me.

Regards,
Wiz

Re: Replacing string characters

Posted: Thu Mar 10, 2022 5:23 pm
by EViews Gareth
There isn't really enough information for us to help here.

The obvious answer is - just replace the whole thing?

Re: Replacing string characters

Posted: Thu Mar 10, 2022 11:24 pm
by Wiz_Ven
Dear Gareth, thank you for your reply. I'll try to be clearer. In practice, I have the following string "e = HPG = C(1) + C(2)*EPUTI + C(3)*INCGM + C(4)*LMSIM + C(5)*HPGM(-1) + C(7)*LMSIM(-1) + C(8)*INCGM(-1)", which is the representation of a previous estimate, which I need to "modify" to use it in a subsequent recursive estimate. From this string, I have to get to this new "e1 = HPG(!i) c EPUTI INCGM LMSIM EPUTI*HPGM(-1) EPUTI*INCGM EPUTI*INCGM(-1) EPUTI*LMSM EPUTI*LMSIM(-1) HPGM(-1) INCGM(-1) LMSIM(-1)" string.

Basically, starting from the string "e" I have to follow the following steps:
1) Replace the left hand size term "HPG =" with "HPG(!i)"
2) Replace the term "C(1)" with "c"
3) Drop all the terms "+"
4) Drop all the terms "C(1)*", "C(2)*",..., "C(8)*"
5) Multiply the term EPUTI with all subsequent terms
6) Place the multiplicative terms after "HPG (!i) c EPUTI INCGM LMSIM"
7) Order the multiplicative terms so that the multiplicative terms between EPUTI and HPGM appear first, then those between EPUTI and INCGM, and finally those between EPUTI and LMSIM.
8) Place the lagged regressors at the end.

In particular, since in my script I will be able to deal with many regressors, it would be optimal a general command for points 5) -8) that takes into account all the possible variables with the same name at each lag. For example, a command that said to multiply EPUTI with all INCGM variables (contemporary and lagged).

This is an example of a command. Basically I only completed the first three points above.

wfcreate u 1 10

string e = "HPG = C(1) + C(2)*EPUTI + C(3)*INCGM + C(4)*LMSIM + C(5)*HPGM(-1) + C(7)*LMSIM(-1) + C(8)*INCGM(-1)"
string e1 = @replace(e, "HPG =", "HPG(!i)")
e1 = @replace(e1, "=", " ")
e1 = @replace(e1, "C(1)", "c")
e1 = @replace(e1, "+", " ")
show e1

Thanks again for your help.

Re: Replacing string characters

Posted: Fri Mar 11, 2022 8:52 am
by Wiz_Ven
Dear Gareth, I have tried to find a solution to this problem. With the following script I can get what I want:

wfcreate u 1 10

string e = "HPG = C(1) + C(2)*EPUTI + C(3)*INCGM + C(4)*LMSIM + C(5)*UCH1GM + C(6)*HPGM(-1) + C(7)*UCH1GM(-1) + C(8)*HPGM(-6) + C(9)*EPUTIM(-12) + C(10)*HPGM(-11) + C(11)*INCGM(-2) + C(12)*INCGM(-1) + C(13)*LMSIM(-11) + C(14)*INCGM(-5) + C(15)*UCH1GM(-3) + C(16)*EPUTIM(-9) + C(17)*LMSIM(-9) + C(18)*LMSIM(-6) + C(19)*INCGM(-6)"
string e1 = @replace(e, "HPG =", "HPG(!i)")
e1 = @replace(e1, "=", " ")
e1 = @replace(e1, "C(1)", "c")
e1 = @replace(e1, "+", " ")

for !i=1 to 40
e1 = @replace(e1, "C("+@str(!i)+")*", " ")
'show e1

next

string e2 = "HPG(!i) c EPUTI"
e2 = e2 + " " + @wkeep(e1, "INCGM")
e2 = e2 + " " + @wkeep(e1, "IN?*)")
e2 = e2 + " " + @wkeep(e1, "HPGM?*)")
e2 = e2 + " " + @wkeep(e1, "LMSIM")
e2 = e2 + " " + @wkeep(e1, "LM?*)")
e2 = e2 + " " + @wkeep(e1, "UCH1GM")
e2 = e2 + " " + @wkeep(e1, "UCH?*)")
e2 = e2 + " " + @wkeep(e1, "EPUT?*)")

for !i=1 to 24

e2 = e2 + " " + "EPUTI"+"*"+ @wkeep(e1, "HPGM(-"+@str(!i)+")"+ " ")
next

e2 = e2 + " " + @wkeep(e1, "INCGM")

for !i=1 to 12

e2 = e2 +" " + "EPUTI"+"*"+ @wkeep(e1, "INCGM(-"+@str(!i)+")"+ " ")
next

e2 = e2 + " " + @wkeep(e1, "LMSIM")

for !i=1 to 12

e2 = e2 +" " + "EPUTI"+"*"+ @wkeep(e1, "LMSIM(-"+@str(!i)+")")
next

e2 = e2 + " " + @wkeep(e1, "UCH1GM")

for !i=1 to 12

e2 = e2 +" " + "EPUTI"+"*"+ @wkeep(e1, "UCH1GM(-"+@str(!i)+")")
next


for !i=1 to 12

e2 = e2 +" " + "EPUTI"+"*"+ @wkeep(e1, "EPUTIM(-"+@str(!i)+")")
next

e2 = @replace(e2, "EPUTI* ", " ")

show e2


However it is not very efficient for two reasons: 1) The number of iterations is randomly selected by putting a sufficiently large number; 2) It does not always work because selecting for example "for !i = 1 to 18" remains a last term "EPUTI*" that the last command line "e2 = @replace (e2," EPUTI* "," ") "fails to delete. At this point my questions are the following:
1) Is there a way to select a maximum number of iterations taking for example the maximum number of lags of the variables according with the starting string "e"?
2) How can I fix the bug related to the presence of a last term "EPUTI*" when I consider a different number of iterations?
3) More generally, is there a "smarter" way to get my "target" result?