Enroll for Certified SAS Base course, training by SAS Certified experts. Find job globally with the help of digital badge, become Certified SAS Base Programmer, get job placement assistance.
2. Concatenating Tables
Suppose you have two or more tables that
have the same columns, and you need to
combine all of the rows into a single table. We
call this concatenating tables.
3. When the columns have the same names, lengths, and types, then
concatenatingtables is simple. You use the DATA step to create a
new table, and simply list the tables that you want to combine in
the SET statement. First, SAS reads all of the rows from the first
table listed in the SET statementand writes them to the new table,
then it reads and writes the rows from the second table, and so on.
In the same DATA step, you can include any other statements that
you need to manipulatethe rows read from all tables.
4. Use the SET statement
when you want to
combine tables with
similardata in one
output table.
Concatenating Tables with
Matching ColumnsDATA output-table;
SET input-table1 input-
table2 ...;
RUN;
any number
of input tables
5. In this example, we have the sashelp.classtableand the class_new
table that includes a few new students. I want to include all
students in a new tablenamed class_current.These two tables
have the same columnsand attributes, so we can list both tables in
the SET statement. The new class_currenttable has 22 rows, which
includes the additional threenew students.
6. Concatenating Tables with
Matching Columnsdata class_current;
set sashelp.class pg2.class_new;
run;
sashelp.class
pg2.class_new
class_current
rows from second table added
after rows from the first table
7. Activity
• Open p205a01.sas from the activities folder and perform the following
tasks:
1. Notice that the SET statementconcatenates the sashelp.class and
pg2.class_new2 tables. Highlight the DATA step and run the selected
code. What differences do you observe between the first 19 rows and the
last 3 rows?
2. Use the RENAME= data set option to change Student to Name in the
pg2.class_new2 table. Highlight the DATA step and run the selected code.
What warning is issued in the log?
3. Highlight the two PROC CONTENTS steps and run the selected code.
What is the length of Name in sashelp.class and Student in
pg2.class_new2?
8. Activity – Correct Answer
1. What differences do you observe between
the first 19 rows and the last 3 rows?
Height and Weight
are missing.
Student names are in
separate columns.
9. Activity – Correct Answer
2. Use the RENAME= data set option to change
Student to Name in the pg2.class_new2
table. Highlight the DATA step and run the
selected code. What warning is issued in the
log?
data class_current;
set sashelp.class
pg2.class_new2(rename=(Student=Name));
run;
WARNING: Multiple lengths were
specified for the variable Name
by input data set(s). This can
cause truncation of data.
10. Activity – Correct Answer
3. Highlight the two PROC CONTENTS steps and
run the selected code. What is the length of
Name in sashelp.class and Student in
pg2.class_new2?
data class_current;
set sashelp.class
pg2.class_new2(rename=(Student=Name));
run;
Name has a
length of 8.
Student has a
length of 9.
11. When multiple tables are listed in a SET statement, as the program
is compiled, columnsfrom the first tablewill be added to the PDV
with their corresponding attributes.When SAS reads the second
table in the SET statement,any columns alreadyin the PDV are not
change. In other words, the LENGTH is already set and cannot be
modified.
13. We can solve this problem simply by using the LENGTH statement.
Here we are explicitlydefining the column Name to be character
with a length of 9. Notice that the LENGTH statement comes before
the SET statement so that it will establishthe attributesof Name in
the PDV.
14. Merging Column Attributes
PDV
Name
$ 9
...other columns...
data class_current;
length Name $ 9;
set sashelp.class
pg2.class_new2(rename=(Student=Name));
run;
Use a LENGTH
statement before
the SET statement
to establishan
appropriatelength.
15. •Best SAS Training & Placement Institute in Pune
•Online & Classroom training with 100 % Free Placement
Assistance
For Enquiry
9960935965 / 9960935600
Mail us - info@aspireit.net
Visit us : http://aspireit.net/
Follow Us