Looking for insight into using 'dim' of the first array to define the number of elements in a second array.
The scenario is when we don't know the number of variables in the raw data and/or want SAS to count them for us
The question is in section 2) below but wanted to provide some context to my inquiry .. hope it's clear enough
Raw Data:
data raw_data;
input fahrenheit1-fahrenheit5;
datalines;
100 75 50 25 0
;
1) Array using {*} and dim for the fahrenheit_array but using celsius1-celsius5 to define the elements of celsius_array
data test (drop=i);
set raw_data;
array fahrenheit_array {*} _all_;
array celsius_array {*} celsius1-celsius5; /* << initial 'blank' elements BASED ON THE KNOWN 5 VARIABLES*/
do i = 1 to dim(fahrenheit_array);
celsius_array{i} = 5/9*(fahrenheit_array{i} - 32); /* << element values defined*/
end;
run;
2) How can we define the number of variables needed for the celsius_array based on dim(fahrenheit_array) ?????
data test (drop=i);
set raw_data;
array fahrenheit_array {*} _all_;
array celsius_array {*} var1-var ???; /* << HOW CAN WE DEFINE THE NUMBER OF VARIABLES HERE BASED ON DIM(FAHRENHEIT_ARRAY) ??? */
do i = 1 to dim(fahrenheit_array);
celsius_array{i} = 5/9*(fahrenheit_array{i} - 32);
end;
run;
many thanks
data raw_data;
input fahrenheit1-fahrenheit5;
datalines;
100 75 50 25 0
;
data _null_;
set raw_data;
array fahrenheit_array {*} _all_ ;
call symputx('n',dim(fahrenheit_array));
stop;
run;
%put &n;
data test (drop=i);
set raw_data;
array fahrenheit_array {*} _all_;
array celsius_array {&n} ; /* call the macrovar n that has the num elements of the array */
do i = 1 to dim(fahrenheit_array);
celsius_array{i} = 5/9*(fahrenheit_array{i} - 32);
end;
run;
data raw_data;
input fahrenheit1-fahrenheit5;
datalines;
100 75 50 25 0
;
data _null_;
set raw_data;
array fahrenheit_array {*} _all_ ;
call symputx('n',dim(fahrenheit_array));
stop;
run;
%put &n;
data test (drop=i);
set raw_data;
array fahrenheit_array {*} _all_;
array celsius_array {&n} ; /* call the macrovar n that has the num elements of the array */
do i = 1 to dim(fahrenheit_array);
celsius_array{i} = 5/9*(fahrenheit_array{i} - 32);
end;
run;
perfect .. thank you !
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.