Re: How to calculate multiple variable in a calculation thru loop/macro

Posted by Bruce Weaver on
URL: http://spssx-discussion.165.s1.nabble.com/How-to-calculate-multiple-variable-in-a-calculation-thru-loop-macro-tp5741015p5741016.html

I think your code basic code can be simplified before you start worrying about looping.  

For example, you can replace this:
   
COMPUTE SAT_NUM_DIS_1=0.
IF (SAT_NUM_AVA_1=1 OR (Q8A_1=1 AND Q8B_1=1)) SAT_NUM_DIS_1=1.

with one COMPUTE command:
   
COMPUTE SAT_NUM_DIS_1 = (SAT_NUM_AVA_1=1) OR (Q8A_1=1 AND Q8B_1=1).

If the expression on the right is true, the variable gets a 1; if it is false, it gets a 0.  I think that is what you want.


And I believe you can replace these two lines:
   
COMPUTE SAT_NUM_OOS_1=$SYSMIS.
IF (SAT_NUM_DIS_1=1) SAT_NUM_OOS_1=0.

with one RECODE command:
   
RECODE SAT_NUM_DIS_1 (1=0) INTO SAT_NUM_OOS_1.


If so, I think that this is what you want to capture in your 1 to 200 loop, right?

* Loop 1.
COMPUTE SAT_NUM_DIS_1 = (SAT_NUM_AVA_1=1) OR (Q8A_1=1 AND Q8B_1=1).
RECODE  SAT_NUM_DIS_1 (1=0) INTO SAT_NUM_OOS_1.
* Loop 2.
COMPUTE SAT_NUM_DIS_2 = (SAT_NUM_AVA_2=1) OR (Q8A_2=1 AND Q8B_2=1).
RECODE  SAT_NUM_DIS_2 (1=0) INTO SAT_NUM_OOS_2.
* Etc.
* Loop 200.
COMPUTE SAT_NUM_DIS_200 = (SAT_NUM_AVA_200=1) OR (Q8A_200=1 AND Q8B_200=1).
RECODE  SAT_NUM_DIS_200 (1=0) INTO SAT_NUM_OOS_200.


Or do you also want to loop through a list of prefixes that you sub in for Q8A and Q8B?  

Thanks for clarifying.  


Satish wrote
Respected Members,

Need your support on below mentioned queries.

Actually I have to perform below mentioned calculation for 200 variables which is very tough to manage in daily basis one by one. Please help me how I can generate 200 different variable for 200 different variables.

variables starting from "_1" to "_200". Prefix for target variable will be "SAT_NUM_DIS_" and only last digit (_1) will will change.

Prefix for existing variable will be different for each of them like "Q8A_" and "Q8B_".

1.

COMPUTE SAT_NUM_DIS_1=0.
IF (SAT_NUM_AVA_1=1 OR (Q8A_1=1 AND Q8B_1=1))SAT_NUM_DIS_1=1.
EXECUTE.

2.

COMPUTE SAT_NUM_OOS_1=$SYSMIS.
IF(SAT_NUM_DIS_1=1)SAT_NUM_OOS_1=0.
EXECUTE.

Thanks in advance

Regards
--
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/).