Hi everyone,
I am very new to the array function so I apologize in advance if my question sounds silly. Is there a way to specify the number of observations in arrays through a list? I am trying to use the following code from @hashman and create 100 views from a huge dataset. I was wondering if there is a way to specify the list of percentages instead listing (5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5).
data _null_ ; array pct p1-p20 (5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5) ; lo = 1 ; do over pct ; cpct + pct ; hi = ceil (cpct * divide (n, 100)) ; call execute (catx (" ", cats ("data v", _i_), "/", cats ("view=v", _i_), ";")) ; call execute (catx (" ", "set have (firstobs=", lo, "obs=", hi, ") ;")) ; call execute ("run ;") ; lo = hi + 1 ; end ; stop ; set have nobs = n ; run ;
Thank you!
Hi @AmirSari,
You can use the abbreviated syntax
array pct p1-p20 (20*5) ;
as described in the documentation of the ARRAY statement (not: function).
Hi @AmirSari,
You can use the abbreviated syntax
array pct p1-p20 (20*5) ;
as described in the documentation of the ARRAY statement (not: function).
Hi @AmirSari
You say you want to create 100 views, but from your code it seems that the desired result is 20 views, each taking 5 pct of the full dataset.
To achieve this, an array is not necessary, You use the array do determine the number of iterations and the number of observations selected in each iteration, and this can be done simpler, so here is a slightly modified version of your code:
data have;
do i = 1 to 10000;
output;
end;
run;
%let splitcount = 20;
data _null_ ;
lo = 1 ;
increment = floor(divide(n, &splitcount));
do i = 1 to &splitcount;
hi = floor(lo) + (increment-1);
call execute (catx (" ", cats ("data v", put(i,8.)), "/", cats ("view=v",put(i,8.)), ";")) ;
call execute (catx (" ", "set have (firstobs=", lo, "obs=", hi, ") ;")) ;
call execute ("run ;") ;
lo = floor(hi) + 1 ;
end ;
stop ;
set have nobs = n ;
run ;
But I wonder how you you are going to use these views, as I cannot imagine any purpose that cannot be solved simpler with a sample from the full dataset or by adding a group variable similar to the view number. Then you can pull the wanted observations with the group number:
* Add group to data;
data want;
set have nobs=n end=eod;
retain group 1;
increment = floor(divide(n, &splitcount));
if _N_ > (increment * group) and not eod then group = group + 1;
run;
* Get data from single group (same as view v3);
data d3;
set want (drop=increment where=(group=3));
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.