Over the holidays I was having a discussion with my cat, Ms. Trixie Lou. A question that often arises during the first programming class is the following: how do I find the variables that are in common to these two or three data sets? As it turns out, Ms. Trixie has just started programming in SAS and had some useful input. She suggested using the CONTENTS procedure to get a list of variables in each table.
As with most students, this was an excellent start. Except the CONTENTS procedure also gives a whole bunch of other information that you have to wade through before you can find just the variable names. Here was my suggestion:
In this example I am trying to determine if the titanic and german data sets (both in the sasuser library) have any variables in common. We use the short option with the CONTENTS procedure to get an alphabetical listing of just the variable names, nice right? But let's not stop there.
Let's add the null title and footnote statements to clear out any leftover titles and footnotes you may have from previous reports.
Also, set the noproctitle option through the output delivery system to suppress the default title of "The Contents Procedure" in our output report (see below.) We do not need to see it.
Finally, by using the two additional ODS statements we create a single nice HTML report so that all the variables appear in the same output window. By default, the variable lists can print to separate reports making them difficult to compare.
It is now very easy to spot that both data sets contain the variable Gender. By the way, if you ran this code in either SAS Enterprise Guide or the latest 9.3 version of SAS, you might not require the ODS statements. Both of those interfaces produce output where your reports could end up in the same window by default.
Now the truth of the matter is there are plenty of fancier ways of answering this same question of finding common variables in data sets. For example, there is a COMPARE procedure that compares data sets. There are also these really cool DICTIONARY tables that you can query with the SQL procedure. But unfortunately Ms. Trixie Lou is telling me that it is her nap time and is demanding a tummy rub. So I will have to see you next time.
Until then, keep on coding better than my kitty.