I'm struggling with a homework question for my SAS class. Using the provided data set "Mice" which contains the weight in ounces of 8 mice, I have to use arrays and a DO loop to create 8 new variables named "Diff1" to "Diff8" that contain the difference in weight between each mouse and the average weight of all 8 mice. I also have to calculate the average weight in the array.
Here is the provided Data:
DATA WORK.Mice;
INFILE DATALINES;
INPUT WtOz1 - WtOz8 @@;
DATALINES;
0.57 0.69 0.87 0.86 0.83 0.63 0.68 0.59
;
Here is my attempt at the question following an example problem. I think I got the right values but they appear as "WtOz1" and so on rather than "Diff1" and I know there's a better way than to just rename the variables. I also don't know how to calculate the average within the array.
DATA MiceMean;
SET Work.Mice;
MeanW= (MEAN(OF WtOz1-WtOz8));
ARRAY Diff{*} WtOz1 - WtOz8;
DO i= 1 TO DIM(Diff);
Diff{i}= MeanW-Diff{i};
Drop i ;
END;
RUN;
Thank you!
Amanda
Amanda,
Everything you're doing is basically correct. You're saying MEAN which is the correct function, and you're using "of" which is the correct way to refer to multiple variables or an array. You're using a variable range, WtOz1 - WtOz2, instead of an array reference. An array reference for an array named WtOzs would be WtOzs[*].
Here's one possible way to code this:
DATA WORK.Mice;
DROP _:;
INFILE DATALINES;
INPUT WtOz1 - WtOz8 @@;
ARRAY WtOzs [*] WtOz1 - WtOz8;
ARRAY DiffOzs [*] DiffOz1 - DiffOz8;
Avg_WtOz = MEAN(of WtOzs[*]);
DO _i = 1 TO DIM(WtOzs);
DiffOzs[_i] = Avg_Wtoz - WtOzs[_i];
END;
DATALINES;
0.57 0.69 0.87 0.86 0.83 0.63 0.68 0.59
;
RUN;
Notice however that I have two arrays. One array is for the values read in, the weights. The other is for the calculation results, the differences. I wouldn't try to do this in one array although you could. If you don't want the weights in your results, then you could just code: DROP WtOz1 - WtOz2.
Since my two arrays are essentially mirror images of one another, a single subscript, _i, can be used to access both. The arrays function in parallel.
Jim
Amanda,
Everything you're doing is basically correct. You're saying MEAN which is the correct function, and you're using "of" which is the correct way to refer to multiple variables or an array. You're using a variable range, WtOz1 - WtOz2, instead of an array reference. An array reference for an array named WtOzs would be WtOzs[*].
Here's one possible way to code this:
DATA WORK.Mice;
DROP _:;
INFILE DATALINES;
INPUT WtOz1 - WtOz8 @@;
ARRAY WtOzs [*] WtOz1 - WtOz8;
ARRAY DiffOzs [*] DiffOz1 - DiffOz8;
Avg_WtOz = MEAN(of WtOzs[*]);
DO _i = 1 TO DIM(WtOzs);
DiffOzs[_i] = Avg_Wtoz - WtOzs[_i];
END;
DATALINES;
0.57 0.69 0.87 0.86 0.83 0.63 0.68 0.59
;
RUN;
Notice however that I have two arrays. One array is for the values read in, the weights. The other is for the calculation results, the differences. I wouldn't try to do this in one array although you could. If you don't want the weights in your results, then you could just code: DROP WtOz1 - WtOz2.
Since my two arrays are essentially mirror images of one another, a single subscript, _i, can be used to access both. The arrays function in parallel.
Jim
It isn't an "and" type question. You create new variables, one way at least, by declaring them in an array definition.
A statement like
Array diff(8);
will create 8 variables named diff1, diff2, and so forth.
You would assign values to each indexed variable inside the do loop though.
I would suggest NOT replacing the values in your weight variables so you can check results.
It might make sense to create a second array of those though, so you are pretty close:
DATA MiceMean; SET Work.Mice; MeanW= (MEAN(OF WtOz1-WtOz8)); ARRAY wt{*} WtOz1 - WtOz8; array diff(8); DO i= 1 TO DIM(Diff); Diff{i}= MeanW-wt{i}; Drop i ; END; RUN;
It is very common to use parallel arrays. Just make sure that they all reference things in the expected order which can be tricky when using less than optimal names for variables.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.