BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
clueless_loops
Calcite | Level 5

Hi,

 

I have worked with SAS for basic tasks and analysis in the past, but I am inexperienced with arrays and do loops.

 

I am having trouble figuring out how to derive two subscores (sums) from data that is arranged as follows:

One variable determines whether a given event is interpreted as being positive or negative ( a value of 1 = the event had a positive effect, and a value of 0 means the event in question had a negative effect).

Then there is another corresponding question that asks the subject to rate the magnitude of the positive or negative event (on a scale of 0 to 3 where 0 means very slight or no effect, and 3 means the positive or negative event had a great effect). In other words, one variable (e.g. “q1_type”) tells you the affective direction/interpretation (positive or negative) and one corresponding variable (e.g. “q1_effect_mag”) tells you the magnitude of the positive or negative. Overall, I’m trying to sum up all the magnitudes of events that were considered positive to derive a positive sum, and also sum up all the magnitudes of events that were considered negative to derive a negative sum.

 

I have the following code that is giving me some problems:

 

data test2;

set test1;

array arr_A q1_type q2_type q3_type q4_type q5_type q6_type q7_type q8_type

                      q9_type q10_type q11_type q12_type q13_type q14_type q15_type;

array arr_B q1_effect_mag q2_effect_mag q3_effect_mag q4_effect_mag q5_effect_mag q6_effect_mag q7_effect_mag q8_effect_mag q9_effect_mag q10_effect_mag q11_effect_mag q12_effect_mag q13_effect_mag q14_effect_mag q15_effect_mag;

positive_sum_subscale = 0;

negative_sum_subscale = 0;

do i = 1 to dim(arr_A);

if arr_A(i) in (1) then positive_sum_subscale = positive_sum_subscale + arr_B(i);

if arr_A(i) in (0) then negative_sum_subscale = negative_sum_subscale +arr_B(i);

end;

run;

 

This code is giving me some values which don’t add up correctly and some missing values for either the positive or negative subscales. Is there a better/easier way to compute these scores than what I am trying at the moment?

 

I am using SAS 9.4

 

Thank you

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@clueless_loops wrote:

Hi,

 

I have worked with SAS for basic tasks and analysis in the past, but I am inexperienced with arrays and do loops.

 

I am having trouble figuring out how to derive two subscores (sums) from data that is arranged as follows:

One variable determines whether a given event is interpreted as being positive or negative ( a value of 1 = the event had a positive effect, and a value of 0 means the event in question had a negative effect).

Then there is another corresponding question that asks the subject to rate the magnitude of the positive or negative event (on a scale of 0 to 3 where 0 means very slight or no effect, and 3 means the positive or negative event had a great effect). In other words, one variable (e.g. “q1_type”) tells you the affective direction/interpretation (positive or negative) and one corresponding variable (e.g. “q1_effect_mag”) tells you the magnitude of the positive or negative. Overall, I’m trying to sum up all the magnitudes of events that were considered positive to derive a positive sum, and also sum up all the magnitudes of events that were considered negative to derive a negative sum.

 

I have the following code that is giving me some problems:

 

data test2;

set test1;

array arr_A q1_type q2_type q3_type q4_type q5_type q6_type q7_type q8_type

                      q9_type q10_type q11_type q12_type q13_type q14_type q15_type;

array arr_B q1_effect_mag q2_effect_mag q3_effect_mag q4_effect_mag q5_effect_mag q6_effect_mag q7_effect_mag q8_effect_mag q9_effect_mag q10_effect_mag q11_effect_mag q12_effect_mag q13_effect_mag q14_effect_mag q15_effect_mag;

positive_sum_subscale = 0;

negative_sum_subscale = 0;

do i = 1 to dim(arr_A);

if arr_A(i) in (1) then positive_sum_subscale = positive_sum_subscale + arr_B(i);

if arr_A(i) in (0) then negative_sum_subscale = negative_sum_subscale +arr_B(i);

end;

run;

 

This code is giving me some values which don’t add up correctly and some missing values for either the positive or negative subscales. Is there a better/easier way to compute these scores than what I am trying at the moment?

 

I am using SAS 9.4

 

Thank you


You should provide examples of the data that "don't add up correctly".

 

Addition using the + with missing values will result in a missing total.

Use the SUM function if you do not want that behavior:

positive_sum_subscale = sum (positive_sum_subscale,  arr_B(i) );

for example. The result will only be missing in the case where all of the variables used as arguments to the SUM function are missing.

View solution in original post

3 REPLIES 3
ballardw
Super User

@clueless_loops wrote:

Hi,

 

I have worked with SAS for basic tasks and analysis in the past, but I am inexperienced with arrays and do loops.

 

I am having trouble figuring out how to derive two subscores (sums) from data that is arranged as follows:

One variable determines whether a given event is interpreted as being positive or negative ( a value of 1 = the event had a positive effect, and a value of 0 means the event in question had a negative effect).

Then there is another corresponding question that asks the subject to rate the magnitude of the positive or negative event (on a scale of 0 to 3 where 0 means very slight or no effect, and 3 means the positive or negative event had a great effect). In other words, one variable (e.g. “q1_type”) tells you the affective direction/interpretation (positive or negative) and one corresponding variable (e.g. “q1_effect_mag”) tells you the magnitude of the positive or negative. Overall, I’m trying to sum up all the magnitudes of events that were considered positive to derive a positive sum, and also sum up all the magnitudes of events that were considered negative to derive a negative sum.

 

I have the following code that is giving me some problems:

 

data test2;

set test1;

array arr_A q1_type q2_type q3_type q4_type q5_type q6_type q7_type q8_type

                      q9_type q10_type q11_type q12_type q13_type q14_type q15_type;

array arr_B q1_effect_mag q2_effect_mag q3_effect_mag q4_effect_mag q5_effect_mag q6_effect_mag q7_effect_mag q8_effect_mag q9_effect_mag q10_effect_mag q11_effect_mag q12_effect_mag q13_effect_mag q14_effect_mag q15_effect_mag;

positive_sum_subscale = 0;

negative_sum_subscale = 0;

do i = 1 to dim(arr_A);

if arr_A(i) in (1) then positive_sum_subscale = positive_sum_subscale + arr_B(i);

if arr_A(i) in (0) then negative_sum_subscale = negative_sum_subscale +arr_B(i);

end;

run;

 

This code is giving me some values which don’t add up correctly and some missing values for either the positive or negative subscales. Is there a better/easier way to compute these scores than what I am trying at the moment?

 

I am using SAS 9.4

 

Thank you


You should provide examples of the data that "don't add up correctly".

 

Addition using the + with missing values will result in a missing total.

Use the SUM function if you do not want that behavior:

positive_sum_subscale = sum (positive_sum_subscale,  arr_B(i) );

for example. The result will only be missing in the case where all of the variables used as arguments to the SUM function are missing.

clueless_loops
Calcite | Level 5

Thank you for your response, ballardw. That seemed to take care of one of the problems with missing values, the other problem of "things not adding up" had to do with a typo in the arrays. In reality the code I used here was just an example that maintained the basic structure of my actual code which has a lot more magnitude and direction variables than the ones I mention here. I used a simpler version of the code here for brevity, but sometimes one makes errors copying and typing in hundreds of variables which cause the arrays to be of unequal lengths. Thankfully, I managed to spot this other problem as well.

ballardw
Super User

@clueless_loops wrote:

Thank you for your response, ballardw. That seemed to take care of one of the problems with missing values, the other problem of "things not adding up" had to do with a typo in the arrays. In reality the code I used here was just an example that maintained the basic structure of my actual code which has a lot more magnitude and direction variables than the ones I mention here. I used a simpler version of the code here for brevity, but sometimes one makes errors copying and typing in hundreds of variables which cause the arrays to be of unequal lengths. Thankfully, I managed to spot this other problem as well.


I can state that I have never had two or more arrays that had parallel structures not match. Honest. Really. Man Wink

 

I find that using the editor to align the text into consistent blocks can help. The key is to get one of the array definitions correct and "pretty" for instance:

array arr_A q1_type  q2_type  q3_type  q4_type  q5_type  q6_type  q7_type  q8_type
            q9_type q10_type q11_type q12_type q13_type q14_type q15_type;

note that I use a code box opened with the forum's {I} icon to preserve plain text layout. The main message windows will reformat text and can make things harder to read. I set all of the variables so that the SUFFIX value of TYPE aligns. If one of them were to be misspelled such as typo instead of type or rype instead of type it would show fairly obviously.

Then after I have ONE of the arrays nice, copy and paste then use search and replace on the suffix text. Don't forget to change the array name. The ctrl-h and "replace in selection" make this easy. So I get a result of

array arr_B q1_effect_mag  q2_effect_mag  q3_effect_mag  q4_effect_mag  q5_effect_mag  q6_effect_mag  q7_effect_mag  q8_effect_mag
            q9_effect_mag q10_effect_mag q11_effect_mag q12_effect_mag q13_effect_mag q14_effect_mag q15_effect_mag;

which is easy to verify that the suffixes all match.

I generally try to keep the variables of a similar length if practical and use Labels to provide more information.

 

I have some project that have multiple arrays like this because I am using them to report on counts, means, upper and lower confidence limits and possibly other statistics in parallel arrays.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 514 views
  • 0 likes
  • 2 in conversation