Dear Community,
The code below is designed to pass data to Python, calculate internal rates of return and return the IRR to SPSS. When I use the SetValueNumeric method of spss.Cursor, the new variable is passed to SPSS as all missing values. I can't figure out why. The correct values are in Python and I can get them into SPSS by printing them to a text file and reading the text file into SPSS. The program and output is copied below. I've also attached a copy of the output file. I'm using SPSS version 29.
I appreciate your help!
Best regards,
Steve O'Byrne
get file = 'data file for dollar weighted returns.sav'.
* select if (gvkey eq '12124').
* list variables = gvkey year horizon basemthnum endmthnum.
set printback on mprint on.
BEGIN PROGRAM PYTHON.
import spss
import numpy_financial as npf
spss.Submit("Frequencies variables = horizon.") # this passes a command to SPSS without transfering any data to Python
dataCursor = spss.Cursor(accessType='w')
dataCursor.AllocNewVarsBuffer(8) # needed to save one numeric variable
allCases = dataCursor.fetchall() # allCases is a list of tuples; each tuple has 542 (= 5 + 537) values
print("Number of cases is ",len(allCases))
gvkey = []
year = []
horizon = []
basemthnum = []
endmthnum = []
irrCases = []
irr_calc = []
for i in range(len(allCases)):
gvkey.append(allCases[i][0])
year.append(allCases[i][1])
horizon.append(allCases[i][2])
basemthnum.append(allCases[i][3])
endmthnum.append(allCases[i][4])
for i in range(len(allCases)):
temp_list = []
irrCases.append(temp_list)
print("Length of irrCases is ",len(irrCases))
for i in range(len(allCases)):
for j in range(len(allCases[i])):
if (j > (basemthnum[i]+3) and j < (endmthnum[i]+5)):
irrCases[i].append(allCases[i][j])
for i in range(len(irrCases)):
irr_calc.append(round(npf.irr(irrCases[i]),6))
dataCursor.SetVarNameAndType(['mthly_irr'],[0])
dataCursor.SetVarFormat('mthly_irr',5,9,6)
dataCursor.CommitDictionary()
for i in range(len(irrCases)):
dataCursor.fetchone()
dataCursor.SetValueNumeric('mthly_irr',irr_calc[i])
dataCursor.CommitCase() # this code creates variable but all values are missing
dataCursor.close()
outfile = open("c:/execcomp/2023_data/dollar weighted returns.txt","w")
for i in range(len(irrCases)):
print(gvkey[i],year[i],horizon[i],basemthnum[i],endmthnum[i],len(irrCases[i]),irr_calc[i],sep=',',file = outfile)
outfile.close()
END PROGRAM.
set printback = on mprint = off.
frequencies variables = mthly_irr.
DATA LIST file = 'c:\execcomp\2023_data\dollar weighted returns.txt' FREE (',')/
gvkey (a7) year horizon basemthnum endmthnum dwrtr_cases mthly_irr.
compute hzn_irr = ((1 + mthly_irr)**(endmthnum - basemthnum)) - 1.
variable labels hzn_irr 'Dollar Weighted Return'.
formats mthly_irr (f7.5) hzn_irr (f8.4).
temporary.
select if (gvkey eq '12124').
list variables = gvkey mthly_irr hzn_irr/cases = 100.
frequencies variables = hzn_irr/format = notable/percentiles = 10 25 50 75 90/statistics = mean maximum minimum.
sort cases gvkey year horizon.
save outfile = 'dollar weighted returns by gvkey year horizon.sav'.
Stephen F. O'Byrne
President
Shareholder Value Advisors Inc
21 Bonnie Way
Larchmont, NY 10538
914-833-5891 cell 914-672-1646