Help using Base SAS procedures

Bring the length of an array to another array in SAS?

Accepted Solution Solved
Reply
Contributor
Posts: 23
Accepted Solution

Bring the length of an array to another array in SAS?

I have a big SAS table, let's describe the columns as, A nd B columns in character format and all other columns are vairable in numerical format (every variable has a different name) with unknow amounth length N, like:

A B Name1 Name2 Name3 ....... NameN

-----------------------------------------------

Char Char Number1 Number2 Number3 ..... NumberN

.................................................

.................................................

The goal is that the numerical array Name1-NameN will sum up downward through the class="B" (By B), So the final table will look like this:

A B Name1 Name2 Name3 .... NameN

----------------------------------------

Char Char Sum1 Sum2 Sum3 ..... SumN

........................................

........................................

To do this sum-up, I described 2 arrays. The first one is:

array Varr {*} _numeric_; /* it reads only numerical columns */ 

Then I described another array with the same length (Summ1-SummN) to do the sum-up process.

The thing is that I can only describe the length of this new array manually. For example, if there are 80 numerical values, then I have to write manually like:

array summ {80} Summ1-Summ80; 

The code works when I write it manually. But instead I want to write something like

array summ {&N} Summ1-Summ&N; /* &N is the dimension of the array Varr */ 

I tried with do-loop and dim(Varr) under the array in many different ways like:

data want;

array Varr {*} _numeric_;

do i=1 to dim(Varr);

N+1 ; end;

%put &N;

array Summ {&N} Summ1-Summ&N;

retain Summ;

if first.B then do i=1 to dim(varr); summ(i)=varr(i) ;end;

else do i =1 to dim(varr);

summ(i) = summ(i) + varr(i) ;

varr(i)=summ(i); end;

drop Summ1-Summ&N;

run;

But it doesn't work. Any idea about how to bring the length of the first array to the second array?


Accepted Solutions
Solution
‎07-01-2015 05:02 AM
Super User
Posts: 6,928

Re: Bring the length of an array to another array in SAS?

An array is defined during compile-time, the dim() function works during runtime of the data step, so you can't use it to define an array.

I'd determine the number of variables in a preceding step, save it in a macro variable and use that for the definition of the new array.

You could extract the count of numeric variables from sashelp.vcolumn (or dictionary.columns in SQL); you could also do

data _null_;

set have (obs=1);

array varr {*} _numeric_;

call symput ('varnum',put(dim(varr),best.));

run;

But you could use proc means without a var statement (so it summarizes all numeric variables automatically) and use OUTPUT OUT=want SUM=;

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Solution
‎07-01-2015 05:02 AM
Super User
Posts: 6,928

Re: Bring the length of an array to another array in SAS?

An array is defined during compile-time, the dim() function works during runtime of the data step, so you can't use it to define an array.

I'd determine the number of variables in a preceding step, save it in a macro variable and use that for the definition of the new array.

You could extract the count of numeric variables from sashelp.vcolumn (or dictionary.columns in SQL); you could also do

data _null_;

set have (obs=1);

array varr {*} _numeric_;

call symput ('varnum',put(dim(varr),best.));

run;

But you could use proc means without a var statement (so it summarizes all numeric variables automatically) and use OUTPUT OUT=want SUM=;

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 23

Re: Bring the length of an array to another array in SAS?

Thanks a lot @KurtBremser  it was a must to know...

☑ This topic is SOLVED.

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

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