@Amanda_Lemon wrote:
Wait, why is it var5 that has a new value? I thought it should be that if the whole set (var 1 through var 5) is new, then increment the counter... no?
The BY has all 5 listed so it's grouped within those variables. Since var5 is the last it uniquely identifies your groups.
Ie If Var2 stays the same it may still be a new group.
BY group processing is incredibly powerful. It allows you to identify groups in your data for specific processing, so you can do the exact same thing for each BY group without loops. Run the following code as an example. You'll get a chart for each make.
Proc sort data=SASHELP.cars out=cars;
By make;
Run;
Proc SGPLOT data=cars;
By make;
Scatter x=mpg_city y=mpg_highway;
Run;
Do you have access to SAS/IML? Run
proc setinit; run;
and look in the SAS log.
You have some good answers on how to create groups of records that the share same set of values.
In terms of comparisons to other languages it is probably better to think of SAS in terms of relational databases. Think of a SAS dataset not as a matrix but as a series of observations of a fixed number of variables. In that case a two dimensional matrix is normally represented by three variables. One to store the actual value and one each for the row and column index.
I am still not sure what your data represents or even what calculation you are trying to do. It kind of looks like your first column is the student id, but I am not sure what your 5 variables represent. Are they the responses of 5 teachers?
data have ;
input student @;
do teacher=1 to 5 ;
input rating @;
output;
end;
cards;
1 3 4 5 2 3
2 3 4 5 1 3
3 3 4 4 3 4
4 3 4 5 1 3
5 4 5 5 5 5
6 4 5 5 5 5
7 3 4 4 3 4
8 4 1 2 4 6
9 3 4 5 2 3
10 4 5 5 5 5
;
Or perhaps they represent the 5 classes that the student had and the values are the ids of the teacher of the class?
data have ;
input student @;
do class=1 to 5 ;
input teacher @;
output;
end;
cards;
1 3 4 5 2 3
2 3 4 5 1 3
3 3 4 4 3 4
4 3 4 5 1 3
5 4 5 5 5 5
6 4 5 5 5 5
7 3 4 4 3 4
8 4 1 2 4 6
9 3 4 5 2 3
10 4 5 5 5 5
;
As for my question about other languages, I guess to be more specific the question would be as follows: which programming language will allow me to convert a data set into a 2-dimensional array and then work easily with such an array?
Thank you!
@Amanda_Lemon wrote:
As for my question about other languages, I guess to be more specific the question would be as follows: which programming language will allow me to convert a data set into a 2-dimensional array and then work easily with such an array?
Thank you!
Within SAS, IML will allow you to do that. But if you have character variables you'll also run into issues, because matrices/arrays are typically numeric. R dataframe will allow you to store mixed types. Python and R are fairly similar when it comes to analysis.
See the article "Reading ALL variables INTO a matrix."
Your example might look like:
proc iml;
use have;
read all var _NUM_ into X[colname=varNames];
close;
print X[colname=varNames];
To get started with SAS/IML, see "Ten tips for learning the SAS/IML language."
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.