turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- SAS Procedures
- /
- Bring the length of an array to another array in S...

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

07-01-2015 04:30 AM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

07-01-2015 05:02 AM

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

Maxims of Maximally Efficient SAS Programmers

All Replies

Solution

07-01-2015
05:02 AM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

07-01-2015 05:02 AM

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

Maxims of Maximally Efficient SAS Programmers

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

07-01-2015 08:03 AM

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