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;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.