I have a table that looks like
Col1 Col2 Col3 Col4 ...
-----------------------------------------
4 2 1 5
I need to calculate some mins and maxes of the columns in my table, however the number of columns will change every time and I need to start at the third column, the name of which also changes. I know there exists the code for static arrays but is there some way I can modify that for my needs like:
ARRAY X{ncol(my_table)-2} Col3-Last;
Thank you
Here is one way
data have;
array x {10};
do i=1 to 100;
do j=1 to dim(x);
x[j]=rand('integer', 1, 10);
end;
output;
end;
run;
proc sql noprint;
select name into :vars separated by ' '
from dictionary.columns
where libname='WORK' & memname='HAVE' & varnum ge 3;
quit;
%put &vars.;
data want;
set have;
array MyArray{*} &vars.;
/* More Code */
run;
@bignate1030 wrote:
I have a table that looks like
Col1 Col2 Col3 Col4 ...
-----------------------------------------
4 2 1 5
I need to calculate some mins and maxes of the columns in my table, however the number of columns will change every time and I need to start at the third column, the name of which also changes. I know there exists the code for static arrays but is there some way I can modify that for my needs like:
ARRAY X{ncol(my_table)-2} Col3-Last;
Thank you
Not anything dynamic but if the variables already exist in your data and the columns are adjacent as you imply you can use the -- (that is 2 dashes) list operator to define the array:
array x col3varname -- lastvarname.
You do not need to specify the size of the array if you use existing variables .
data example; set sashelp.class; array m age -- weight; x= max(of m(*)); y= min(of m(*)); run;
The variables already exist but I don't know how many there are. I am analyzing variables at different time frames, so I have a table that looks like this:
Person Event Var_X_100_Days Var_X_6_Months.
2 3 1 2
And I can calculate min, max for that variable. However once I've done that I need to do the same for Var_Y. But the table for Var_Y looks like
Person Event Var_Y_6_Months Var_Y_1_Year Var_Y_2_Years Var_Y_3_Years
3 3 12 4 1 6
So I can't define a static range such as Var_X_100_Days -- Var_X_6_Months because the first and last variables will almost always have different names depending on what variable I am analyzing. However I know that the first variable will always be the third column and the last variable will always be the last column.
@bignate1030 have you tried my code?
Also, if the variables of interest have a common prefix, you can simply do this
data want;
set have;
array MyArray{*} Var:;
/* More Code */
run;
@bignate1030 wrote:
The variables already exist but I don't know how many there are. I am analyzing variables at different time frames, so I have a table that looks like this:
Person Event Var_X_100_Days Var_X_6_Months.
2 3 1 2
And I can calculate min, max for that variable. However once I've done that I need to do the same for Var_Y. But the table for Var_Y looks like
Person Event Var_Y_6_Months Var_Y_1_Year Var_Y_2_Years Var_Y_3_Years
3 3 12 4 1 6
So I can't define a static range such as Var_X_100_Days -- Var_X_6_Months because the first and last variables will almost always have different names depending on what variable I am analyzing. However I know that the first variable will always be the third column and the last variable will always be the last column.
If Person and Event are character variables then you can use:
Array m _numeric_;
Otherwise you're going to have to use something like @PeterClemmensen shows.
Or possibly reconsider how your data is structured or generating names like Var_Y_6_Months
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.