SAS

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