## Summing Variables in an Array

Solved
Frequent Contributor
Posts: 79

# Summing Variables in an Array

Hello everyone,

I have an array with eight dichotomous (yes,no) variables and I want to sum the variables such that I get a range of scores 0-8 (0 being none of them have any 'yes's' and 8 being all of the variables are yes. Then for any of the scores lower than 4 I would like to make missing. I am getting the SUM of all the variables as a dichotomous and not an 8 level score. Is there a better way to code this?

ARRAY ACE_ARRAY[8]  ACEPRISN2 ACEDIVRC2 ACEDEPRS2 ACESXAB ACESUBS ACEPUNCH2 ACESWEAR2 ACEHURT2;

SUM_ACESCORE = sum(of ACE_ARRAY[8]);

IF SUM_ACESCORE <= 4 then SUM_ACE = ' ';

Thank you,

Donald S.

Accepted Solutions
Solution
‎08-03-2015 01:20 PM
Regular Contributor
Posts: 194

## Re: Summing Variables in an Array

I don't think the expression "of ACE_ARRAY[8]" is going to work for you like your are thinking it will.  NOW if the variables are always going to be adjacent in the dataset-- i.e. they have consecutive variable numbers (see proc contents) -- then you could use the syntax "of ACEPRISN2 -- ACEHURT2" (that's a double dash) and that would be the appropriate use of the "of" operator.

If you use the array then you want to use a do loop:

SUM_ACESCORE = 0;
do i=1 to 8; drop i;
SUM_ACESCORE+ACE_ARRAY;
end;

All Replies
Solution
‎08-03-2015 01:20 PM
Regular Contributor
Posts: 194

## Re: Summing Variables in an Array

I don't think the expression "of ACE_ARRAY[8]" is going to work for you like your are thinking it will.  NOW if the variables are always going to be adjacent in the dataset-- i.e. they have consecutive variable numbers (see proc contents) -- then you could use the syntax "of ACEPRISN2 -- ACEHURT2" (that's a double dash) and that would be the appropriate use of the "of" operator.

If you use the array then you want to use a do loop:

SUM_ACESCORE = 0;
do i=1 to 8; drop i;
SUM_ACESCORE+ACE_ARRAY;
end;
Frequent Contributor
Posts: 79

## Re: Summing Variables in an Array

Thank you PhilC, I believe this code works the way I want it to. From the proc freq, It seems odd to me that the Score starts at 8 and goes to 16. See below

The FREQ Procedure

 8 9 10 11 302 0.29 302 0.29 1071 1.04 1373 1.33 2135 2.07 3508 3.4 3447 3.34 6955 6.74 5300 5.14 12255 11.87 7819 7.58 20074 19.45 12048 11.67 32122 31.13 21507 20.84 53629 51.96 49574 48.04 103203 100

Can I assume that the level "8" is 8/8 yes's, level "7" is 7/8 "yes's"  etc. and that level "16" is "no yes's"?

Thank you!

Regular Contributor
Posts: 194

## Re: Summing Variables in an Array

The variables could be dichotomous, but have the values 1 and 2?  It's not clear to me.  We need a ~10 row sample of the data, a proc contents, and the code you used to add up the variables.

Frequent Contributor
Posts: 79

## Re: Summing Variables in an Array

I am going to be doing my regression analysis in SUDAAN since it handles complex survey data faster and requires to be coded 1/2 instead of 0/1- yes, it is a pain. This is un-redacted data so I don't think I am allowed to share this information- will have to ask my boss. BUT I did just do some 2X2 tables to see if everything matches up and it appears to for the rows I have looked at so far.

Regular Contributor
Posts: 194