Solved
Contributor
Posts: 55

# Dynamic array elements

How can I produce a dataset whose end array number is dynamic?

Field arraynum is the number of "nums" I'd like to add.

data have;
input id arraynum num1 num2 result;
cards;
1 2 3 8 11
2 1 7 8 7
;
run;

Accepted Solutions
Solution
‎07-03-2016 11:29 AM
Contributor
Posts: 62

## Re: Dynamic array elements

Hi

this was inspired by http://stackoverflow.com/questions/10798393/dynamic-variable-names-in-sas

data have;
input id arraynum num1 num2 result;
cards;
1 2 3 8 11
2 1 7 8 7
;
run;

data want (drop=i);
set have;
array nums(*) num: ;
do i=1 to arraynum;
tot=sum(of tot,nums(i));
end;
run;

All Replies
Super User
Posts: 10,255

## Re: Dynamic array elements

A dataset has the same structure for all observations; you can't have a dataset with different number of columns in different observations.

And an array is created at data step compile time; it cannot change its size during data step execution time.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 10,779

## Re: Dynamic array elements

What are you trying to accomplish ? You want create different datasets with different obs and different variables ?
Solution
‎07-03-2016 11:29 AM
Contributor
Posts: 62

## Re: Dynamic array elements

Hi

this was inspired by http://stackoverflow.com/questions/10798393/dynamic-variable-names-in-sas

data have;
input id arraynum num1 num2 result;
cards;
1 2 3 8 11
2 1 7 8 7
;
run;

data want (drop=i);
set have;
array nums(*) num: ;
do i=1 to arraynum;
tot=sum(of tot,nums(i));
end;
run;

☑ This topic is solved.