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

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

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

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;

View solution in original post

2 REPLIES 2
novinosrin
Tourmaline | Level 20

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;
Rampsas1
Obsidian | Level 7

perfect ..  thank you !

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1022 views
  • 0 likes
  • 2 in conversation