It is a useful exercise to solve the same problems in SAS as we did in SQL using the dataset procedure.
Below is an example is the same as in the SQL section. It is a database containing two tables, customers and purchase order. The Customer table has customerid (the unique identifier for the customer), name (The customer’s name) and age (the customer’s age). The PurchaseOrder table has POID (the unique id for the purchase order), customerid (which refers back to the customer who made the purchase) and Purchase (what the purchase was).
Example: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
SET
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
DATA output SET MyDataBase.Customer; RUN: |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||
PROC SORT
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
DATA output SET MyDataBase.Customer; RUN: PROC SORT DATA = output BY age; RUN; |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||
IF
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
DATA output SET MyDataBase.Customer; IF age eq 18; RUN: |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||
MERGE
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
PROC SORT DATA = MyDataBase.Customer BY CustomerID; RUN;
PROC SORT DATA = MyDataBase.PurchaseOrder BY CustomerID; RUN;
DATA output MERGE MyDataBase.Customer (in = a) MyDataBase.PurchaseOrder (in = b); By CustomerID; if a and b; RUN: |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||
MERGE with a condition
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
PROC SORT DATA = MyDataBase.Customer BY CustomerID; RUN;
PROC SORT DATA = MyDataBase.PurchaseOrder BY CustomerID; RUN;
DATA output MERGE MyDataBase.Customer (in = a) MyDataBase.PurchaseOrder (in = b); By CustomerID; if a; RUN: |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||
FIRST (Group BY)
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
proc freq data = mydata_lkup; tables col2; ______________or__________________ PROC SORT DATA = output; BY CustomerID; RUN;
DATA outputSummary; Do UNTIL (LAST.CustomerID); SET output ; BY CustomerID; orders = orders + 1; IF LAST.CustomerID THEN OUTPUT; END; KEEP Name Age Orders; RUN; |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||
UPDATE
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
DATA Customer;SET Customer; If CustomerID = 1 Age = 26; Run; |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||
INSERT
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
DATA temp; DO i = 1 to 1; CustomerID = 6; Name = Terry ; Age = 50; OUTPUT; END; RUN;
DATA Customer; SET Customer temp; RUN; |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||
DELETE
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
DATA Customer;SET Customer; If CustomerID = 1 DELETE; Run; |
|