APPEND DATASETS USING SET | MULTIPLE SET | PROC APPEND | PROC SQL UNION

Here we are going to Learn how to Combine/Stack/Append Datasets in SAS, See below Picture to understand- What is Append


Append Datasets In SAS Datahark

SAS has multiple Methods to append datasets, these includes - 

  • Using Set in Datastep
  • Proc Append
  • Proc SQL
  • Merge

Example 1 - Using Set in Datastep 

Data Male;
Input Name $ Age;
Datalines;
Mark 45
Peter 60
Jack 20
;
run;

Data Female;
Input Name $ Age;
Datalines;
Lisa 45
Jane 60
Julie 20
Ruma 42
;
run;

Data Combined;
set Male Female;
run;

Append with Set

Lets See what happen if we write multiple SET statements - 

Data Combined;
set Male;
set Female;
run;

Multiple Set Statements

Here First it copied Male Dataset into Combined (All Three rows) and then Override all three rows of combined dataset with Female Dataset's First Three rows

Example 2 - Using PROC APPEND

Data Male;
Input Name $ Age;
Datalines;
Mark 45
Peter 60
Jack 20
;
run;

Data Female;
Input Name $ Age;
Datalines;
Lisa 45
Jane 60
Julie 20
Ruma 42
;
run;

Proc Append Base= Male Data=Female;
run;

Proc Print Data=Male;
run;

Proc Print Data=Female;

Data Male;
Input Name $ Age;
Datalines;
Mark 45
Peter 60
Jack 20
;
run; 

Data Female;
Input Name $ Age;
Datalines;
Lisa 45
Jane 60
Julie 20
Ruma 42
;
run;

Proc Append Base= Male Data=Female;
run;
Proc Print Data=Male;
run;
Proc Print Data=Female;
run; 

PROC Append

The above proc appended rows of Female Dataset into Male Dataset. It is faster than SET statement as here the second dataset is just appended to the first one and the SAS do not have to process the first dataset in the PDV, Essentially what happens with set or merge is that you bring both the datasets into PDV and then setting is done, the append does not do that it just add the second dataset to base one with out bringing the base dataset to PDV.

Few points for Proc Append - 

  • If PROC APPEND cannot find the BASE= data set, SAS creates it
  • If a DATA= data set is not specified, SAS uses the most recently created SAS data set
  • If appending data sets with different variables or attributes, use the FORCE option. This option must be used to prevent a syntax error.

Example 3 - Using PROC SQL - UNION

Data Male;
Input Name $ Age;
Datalines;
Mark 45
Peter 60
Jack 20
;
run;

Data Female;
Input Name $ Age;
Datalines;
Lisa 45
Jane 60
Julie 20
Ruma 42
;
run;

PROC SQL;
CREATE TABLE COMB_MF AS 
SELECT * FROM MALE
UNION CORR
SELECT * FROM FEMALE;
QUIT;

It will give same output as Example 1 

Here UNION Keyword is used to combined dataset and CORR keyword used to tell SAS that Append datasets based on the Column Name, If we dont use this Keyword PROC SQL will append based on the column sequence.

Post a Comment

0 Comments