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

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Satish
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
Reply | Threaded
Open this post in threaded view
|

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

Bruce Weaver
Administrator
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/).
Reply | Threaded
Open this post in threaded view
|

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

Art Kendall
In reply to this post by Satish
I notice in your syntax that you use "=" both as (1) an assignment operator and (2) as a logical operator.

IFF done properly that will work.  However, 50 years of using SPSS and helping others use SPSS has taught me that one will shoot oneself in the foot less often if one uses the character logical operators {EQ, LT, LE, etc.} rather than the symbolic logical operators {=, <, <=, etc}.

In reading between the lines (what David call ESPSS) it seems like you may be attempting to get a frequency count across a set of variables?

That is given a set of variables (a mrset), you would like to see a table that that has a list of all the values that occur in any of the variables.

CTABLES is one way to get a frequency count across a set of variables.


It is helpful that you posted your attempted syntax.  
A more detailed explanation of what you are trying to do, will frequently let  list members relate what you are trying to do to existing SPSS procedures.

Many users find it helpful to cobble up a small example dataset, and a small example of what you would like to do.
Art Kendall
Social Research Consultants