This SAS code uses the SQL procedure (`PROC SQL`) to...
This SAS code uses the SQL procedure (PROC SQL
) to create a new dataset (cnt_ds
) by performing a LEFT JOIN
operation between two existing datasets (survcnt_ds
as a
and defcnt_ds
as b
). Below is an explanation of what the code does step-by-step:
-
create table cnt_ds as
: This line specifies that the result of the SQL query will be saved as a new dataset namedcnt_ds
. -
select
clause: This specifies the columns to include in the resulting dataset:"current_quarter"d as current_quarter format=date9.,
: The value"current_quarter"d
is selected, assigned the column namecurrent_quarter
, and formatted usingDATE9.
format (e.g.,01JAN2023
).next_quarter_num as next_quarter format=date9.,
: The columnnext_quarter_num
is selected from the input datasets, renamed asnext_quarter
, and formatted using theDATE9.
format.coalesce(a.distinct_entity_count,0) as survived_entity_cnt,
: The columndistinct_entity_count
fromsurvcnt_ds
(a
) is selected. TheCOALESCE
function ensures that if this column contains aNULL
value,0
is used instead. The result is renamed assurvived_entity_cnt
.coalesce(b.distinct_entity_count,0) as default_entity_cnt
: Similarly, the columndistinct_entity_count
fromdefcnt_ds
(b
) is selected. TheCOALESCE
function ensures that anyNULL
value is replaced with0
, and the result is renamed asdefault_entity_cnt
.
-
from survcnt_ds as a
: Specifies the first input dataset,survcnt_ds
, and gives it the aliasa
. -
left join defcnt_ds as b on 1=1
: Specifies aLEFT JOIN
betweensurvcnt_ds
(a
) anddefcnt_ds
(b
). However, the conditionon 1=1
means there is no specific joining condition, resulting in a Cartesian product of the two datasets where all rows ina
are joined with all rows inb
, limited by the structure of aLEFT JOIN
. -
quit;
: Ends the SQL query.
Purpose:
The code creates a new dataset (cnt_ds
) that combines columns and values from survcnt_ds
and defcnt_ds
. Specifically, it calculates two measures:
survived_entity_cnt
: The count of surviving entities fromsurvcnt_ds
(defaults to 0 if no data exists).default_entity_cnt
: The count of defaulted entities fromdefcnt_ds
(defaults to 0 if no data exists).
The output will contain all rows from survcnt_ds
and corresponding aggregate values from defcnt_ds
.