Re: How to calculate (sum) from multiple variable thru loop, if variable are not in sequential order
Posted by
Bruce Weaver on
Jul 13, 2022; 1:47pm
URL: http://spssx-discussion.165.s1.nabble.com/How-to-calculate-sum-from-multiple-variable-thru-loop-if-variable-are-not-in-sequential-order-tp5741009p5741011.html
Hello Satish. Using the data you attached (thanks for that), the following code generated no errors or warnings.
COMPUTE total_share1 = 0.
DEFINE !TSum ().
!DO !t = 1 !TO 186.
!LET !v = !CONCAT("Q6I_T_",!t).
IF NOT MISSING(!v) total_share1 = total_share1+!v.
!DOEND.
!ENDDEFINE.
SET MPRINT ON.
!TSum.
SET MPRINT OFF.
DESCRIPTIVES total_share1.
Notice that I used IF where you used COMPUTE to ensure that I did not try to add any missing variables to the running sum. I also did not understand your !invars variable with SUM(). Why is that needed?
If this revised code gives the result you want, note that you can comment out or remove the two SET MPRINT lines once you know everything is working correctly.
HTH.
Satish wrote
Respected member,
I have a query please help me on this as soon as possible.
I have set of 314 variable starting from Q6I_T_1 to Q6I_T_246 and Q6I_T_1001 to Q6I_T_1068. But all these variables are not in order because we have other set of variable in between. I have tried below mentioned syntax and it's working fine if I am putting range around 100. when i am putting range till 186 or more it's not calculating anything.
note : I have few varibles where data is missing.
Can you help me how i can get total sum of all variables starting from Q6I_T_1 to Q6I_T_1068 . it will great help.
COMPUTE total_share1 = 0.
DEFINE !TSum ().
!DO !t = 1 !TO 100.
!LET !XSum = !CONCAT("Q6I_T_",!t).
!LET !invars = !CONCAT("sum(",!XSum,")").
COMPUTE total_share1 = total_share1+!invars.
!DOEND.
!ENDDEFINE.
!TSum.
EXECUTE.
thanks in advance.
Regards
Test.savSAT
--
Bruce Weaver
bweaver@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/"When all else fails, RTFM."
PLEASE NOTE THE FOLLOWING:
1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above.
2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (
https://listserv.uga.edu/).