BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
AmirSari
Quartz | Level 8

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!

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

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).

View solution in original post

3 REPLIES 3
FreelanceReinh
Jade | Level 19

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).

ErikLund_Jensen
Rhodochrosite | Level 12

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;
AmirSari
Quartz | Level 8
Thank you for taking the time and providing an alternative solution.
I want 100 views each taking 1 pct but 20 views taking 5 pct each was just for example. Each view represents a dataset that I can use afterward. So, instead of splitting the dataset into 100 smaller datasets and wasting disk space, I create 100 views.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 362 views
  • 2 likes
  • 3 in conversation