BookmarkSubscribeRSS Feed
shidehrafigh
Fluorite | Level 6

Hi

 

I was searching for a macro to split my data into smaller data sets and found the following program on stackoverflow.  The code works but I still have the following questions:

1- how SAS creates data want1 while in the program it says: 

%IF &I. > 1 %THEN %DO; ELSE %END;

2- why there is no % before Else?

3- how SAS decides to start data want2 from 250001?

 

thank you

Shideh

 



%MACRO SPLIT(DATASET); %LET DATASET_ID = %SYSFUNC(OPEN(&DATASET.)); %LET NOBS = %SYSFUNC(ATTRN(NLOBS,&DATASET_ID.)); %LET NB_DATASETS = %SYSEVALF(&NOBS. / 250000, CEIL); DATA %DO I=1 %TO &NB_DATASETS.; WANT&I. %END;; SET &dataset; %DO I=1 %TO &NB_DATASETS.; %IF &I. > 1 %THEN %DO; ELSE %END; IF _N_ LE 2.5E5 * &I. THEN OUTPUT WANT&I.; %END; RUN; %MEND SPLIT;

 

3 REPLIES 3
data_null__
Jade | Level 19
ELSE is the data step code generated by the %IF. For values of &I gt 1 the code is ELSE IF _N_ ...
Reeza
Super User

This part creates the data set names to be output, so it becomes Want1- want&nb_datasets. 

 

DATA 
  %DO I=1 %TO &NB_DATASETS.;
    WANT&I. 
  %END;;

 

 


@shidehrafigh wrote:

 

2- why there is no % before Else?

There should be one.

 This creates the ELSE IF, so it's not just IF statements. This means the processing is faster. 


@shidehrafigh wrote:

 

3- how SAS decides to start data want2 from 250001?

 


IF _N_ LE 2.5E5 * &I. THEN OUTPUT WANT&I.;

IF _n_ <= 250000*(2) then output = want2;

 

So this controls the output and says if the value is less than 500000 then it will go to data set want2.

 

Here's my solution to this problem:

 

/*This macro splits a data set into data sets of size N. 
The parameters requried are:

1. DSN = input data set name, such as sashelp.cars. 
   The libname should be included unless the data set
   is in the work library.
2. Size = Number of records to be included in each data 
   set. Note that the last data set will be truncated, 
   ie if only 28 records are available only 28 will be 
   written to the output data set.
3. outDsnPrefix = Name of output data sets, will be indexed as 
      outDSNPrefix1
      outDSNPrefix2
      outDSNPrefix3
*/

%macro split (dsn=, size=, outDsnPrefix=Split);

    %*Get number of records and calculate the number of files needed;
    data _null_;
        set &dsn. nobs=_nobs;
        call symputx('nrecs', _nobs);
        n_files=ceil(_nobs/ &size.);
        call symputx('nfiles', n_files);
        stop;
    run;

    %*Set the start and end of data set to get first data set;
    %let first=1;
    %let last=&size.;
    
    %*Loop to split files;
    %do i=1 %to &nfiles;
    
        %*Split file by number of records;
        data &outDsnPrefix.&i.;
            set &dsn. (firstobs=&first obs=&last);
        run;

        %*Increment counters to have correct first/last;
        %let first = %eval(&last+1);
        %let last = %eval((&i. + 1)*&size.);
    %end;
%mend split;

*Example call;
*After running this, you should find 9 data sets named Split1-Split9;
%split(dsn=sashelp.cars, size=50, outDsnPrefix=Split);


There are also other simpler solutions entirely in a data step here. 

 

Tom
Super User Tom
Super User

Turn on the MPRINT option and run the macro.

Look at the generated code and you will see that the ELSE keyword is part of the code that the macro is generating.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 2302 views
  • 5 likes
  • 4 in conversation