Skip to main content

SAS Base Programmer Certification Training by Certified Experts

Page 1

How to concatenating SAS Data Sets - Concatenating Two or More Data in SAS.

BY


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.


When the columns have the same names, lengths, and types, then concatenating tables 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 statement and 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 manipulate the rows read from all tables.


Concatenating Tables with DATA output-table; Matching Columns SET input-table1 inputtable2 ...; RUN; Use the SET statement when you want to combine tables with similar data in one output table.

any number of input tables


In this example, we have the sashelp.class table and the class_new table that includes a few new students. I want to include all students in a new table named class_current. These two tables have the same columns and attributes, so we can list both tables in the SET statement. The new class_current table has 22 rows, which includes the additional three new students.


Concatenating Tables with Matching Columns data class_current; set sashelp.class pg2.class_new; run;

sashelp.class

class_current

pg2.class_new rows from second table added after rows from the first table


Activity • Open p205a01.sas from the activities folder and perform the following tasks: 1. Notice that the SET statement concatenates 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?


Activity – Correct Answer 1. What differences do you observe between Student names are in the first 19 rowsseparate and columns. the last 3 rows?

Height and Weight are missing.


Activity – Correct Answer 2. Use the RENAME= data set option to change Student data class_current; to Name in the pg2.class_new2 set sashelp.class pg2.class_new2(rename=(Student=Name)); table. Highlight the DATA step and run the run; selected code. What warning is issued in the WARNING: Multiple lengths were log? specified for the variable Name by input data set(s). This can cause truncation of data.


Activity – Correct Answer 3. Highlight the two PROC CONTENTS steps and run the selected code. What is the length of Name has a lengthStudent of 8. data class_current; Name in sashelp.class and in set sashelp.class pg2.class_new2(rename=(Student=Name)); pg2.class_new2? run; Student has a length of 9.


When multiple tables are listed in a SET statement, as the program is compiled, columns from the first table will be added to the PDV with their corresponding attributes. When SAS reads the second table in the SET statement, any columns already in the PDV are not change. In other words, the LENGTH is already set and cannot be modified.


Setting Column Attributes data class_current; set sashelp.class pg2.class_new2(rename=(Student=Name)); run;

PDV Name $8

...other columns... Column lengths are determined by the first table listed in the SET statement.


We can solve this problem simply by using the LENGTH statement. Here we are explicitly defining 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 establish the attributes of Name in the PDV.


Merging Column Attributes data class_current; length Name $ 9; set sashelp.class pg2.class_new2(rename=(Student=Name)); run;

PDV Name $9

...other columns...

Use a LENGTH statement before the SET statement to establish an appropriate length.


•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


Turn static files into dynamic content formats.

Create a flipbook
SAS Base Programmer Certification Training by Certified Experts by Aspire Techsoft - Issuu