Table: Person
+-------------+---------+ | Column Name | Type | +-------------+---------+ | PersonId | int | | FirstName | varchar | | LastName | varchar | +-------------+---------+ PersonId is the primary key column for this table.
Table: Address
+-------------+---------+ | Column Name | Type | +-------------+---------+ | AddressId | int | | PersonId | int | | City | varchar | | State | varchar | +-------------+---------+ AddressId is the primary key column for this table.
Use data step for a report that provides the following information for each person in the Person table, regardless if there is an address for each of those people:
FirstName, LastName, City, State
Do something like this (just made some data up)
data Person;
input PersonID FirstName $ LastName $;
datalines;
1 Peter Smith
2 Otto Burch
3 Paul Johnson
4 William Jones
5 Allan Brown
;
data Address;
input AddressID PersonID City $ State $20.;
datalines;
72 1 Houston Texas
44 3 Miami Florida
87 5 Seattle Washington
;
proc sort data=Person; by PersonID; run;
proc sort data=Address; by PersonID; run;
data want(drop=PersonID AddressID);
merge Person(in=p) Address;
by PersonID;
if p;
run;
Do something like this (just made some data up)
data Person;
input PersonID FirstName $ LastName $;
datalines;
1 Peter Smith
2 Otto Burch
3 Paul Johnson
4 William Jones
5 Allan Brown
;
data Address;
input AddressID PersonID City $ State $20.;
datalines;
72 1 Houston Texas
44 3 Miami Florida
87 5 Seattle Washington
;
proc sort data=Person; by PersonID; run;
proc sort data=Address; by PersonID; run;
data want(drop=PersonID AddressID);
merge Person(in=p) Address;
by PersonID;
if p;
run;
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.