Help using Base SAS procedures

Summing Variables in an Array

Accepted Solution Solved
Reply
Contributor
Posts: 69
Accepted Solution

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: 156

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;

View solution in original post


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

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;
Contributor
Posts: 69

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

       
83020.293020.29
910711.0413731.33
1021352.0735083.40
1134473.3469556.74
1253005.141225511.87
1378197.582007419.45
141204811.673212231.13
152150720.845362951.96
164957448.04103203100.00

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: 156

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.

Contributor
Posts: 69

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: 156

Re: Summing Variables in an Array

No pain is involved, if you see the data as 1's and 2's then its not odd, at all, that the range is 8 to 16.  Thanks for flagging my responses as helpful.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 333 views
  • 0 likes
  • 2 in conversation