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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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