Help using Base SAS procedures

How to Stack Multiple Datasets

Reply
N/A
Posts: 0

How to Stack Multiple Datasets

I have 200 datasets:d1,d2,d3...d200, each with only one observation, I want to stack them into one dataset, I tried the codes below but it didn't work:

data final;
set d1-d200;
run;

I appreciate your advice in having it work out.

Thanks!
Super Contributor
Super Contributor
Posts: 3,174

Re: How to Stack Multiple Datasets

You must list each file separately on the SET statement. A utility SAS macro to generate the SET parameters can look something like:

%MACRO GENSET(FILEPFX=, LOW=, HIGH=);
%DO CYC=&LOW %TO &HIGH;
&FILEPFX&CYC
%END;
%MEND GENSET;

DATA FINAL;
SET %GENSET(FILEPFX=D,LOW=1,HIGH=200);
RUN;

Scott Barry
SBBWorks, Inc.


SAS Macro Language: Reference
Introduction to the Macro Facility
http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/a002293969.htm

SAS Macro Programming for Beginners
Susan J. Slaughter, Avocet Solutions, Davis, CA
Lora D. Delwiche, Delwiche Consulting, Winters, CA
http://www2.sas.com/proceedings/sugi29/243-29.pdf
N/A
Posts: 0

Re: How to Stack Multiple Datasets

It works! Thank you very much Scott.
Trusted Advisor
Posts: 2,113

Re: How to Stack Multiple Datasets

Beickham,

Just an FYI. While Scott's solution works for 200 datasets and your immediate problem is solved, it does not scale to 2000, say. If requires that all of the datasets be open at once and the operating system generally limits the number of "file handles" available (varies by OS).

You could re-write the macro to handle an arbitrary number of datasets, but it could take a lot longer to run.

Doc Muhlbaier
Duke
N/A
Posts: 0

Re: How to Stack Multiple Datasets

I recently came across the parameter OPEN=DEFER for the SET statement, which might get around the problem of too many files open at once.
Senior User
Posts: 1

Re: How to Stack Multiple Datasets

This technique tends to work quite well.

 


data final;
set d:;
run;

 

data output.combined;
set output.final_:;
run;

Ask a Question
Discussion stats
  • 5 replies
  • 1838 views
  • 1 like
  • 4 in conversation