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?
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=;
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=;
Thanks a lot @KurtBremser it was a must to know...
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.