sas proc sql – missing values

Here’s a little trick to give a elegantly default your missing values when you do a merge using PROC SQL.

DATA A;
	CATEGORY = 'A'; NAME = 'CAT-A'; OUTPUT;
	CATEGORY = 'B'; NAME = 'CAT-B'; OUTPUT;
RUN;

DATA B;
	CATEGORY = 'A'; OUTPUT;
	CATEGORY = 'B'; OUTPUT;
	CATEGORY = 'C'; OUTPUT;
RUN;

We want to get the category name from dataset A. But in case we don’t find it, we want to output ‘Unknown’ (instead of the default blank).

The trick is to use the COALESCE function, which returns the first non-missing value among it’s arguments:

PROC SQL;
	CREATE TABLE TEST AS
	SELECT T0.*, COALESCE(T1.NAME, 'UNKNOWN') AS NAME FORMAT $7.
	FROM
	B AS T0
	LEFT JOIN A AS T1
	ON T0.CATEGORY = T1.CATEGORY;
QUIT;

Capture

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s