DATA Step, Macro, Functions and more

Dynamic array elements

Accepted Solution Solved
Reply
Contributor
Posts: 55
Accepted Solution

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: 41

Re: Dynamic array elements

Posted in reply to angeliquec

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;

View solution in original post


All Replies
Super User
Posts: 7,771

Re: Dynamic array elements

Posted in reply to angeliquec

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
Super User
Posts: 10,023

Re: Dynamic array elements

Posted in reply to angeliquec
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: 41

Re: Dynamic array elements

Posted in reply to angeliquec

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.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 321 views
  • 1 like
  • 4 in conversation