Code: coalesce(argument1,….argumentn)

If you have two columns with the same details but in one column there are missing values and in other, there are non-missing values at some places and vice-versa, then coalesce function returns the value of the first nonmissing argument. This can also be taken from another table column by simple joins.

For example, in the table below, I extracted the data which has missing account ID.

irstNameLastNameIncomeAccountIDDateTimeMerchantIDAmount
  ..07MAY18:15:35:0254205858.79
GarySienkiewicz67210.91101015956516SEP18:14:57:08568268107.16
  ..24FEB18:17:27:42562326370.53
SergioLefeld86859.07101036733017OCT18:11:02:3852557621.02
SergioLefeld86859.07101036733015MAY18:17:54:2154205823.39
  ..18OCT18:17:52:5154994037.24
JohnOliver43623.75202001288723FEB18:09:25:37525576108.22
IvaBower67949.96303008522427JUL18:12:05:4852557626.1
JanetSienkiewicz50111.59303010194218SEP18:12:13:4054994037.38
OlgaComstock31896.96303016520711MAR18:10:07:14580881319.95
  ..13FEB18:16:48:05513178115.48
  ..27JAN18:13:52:355361231.76
AdaVieyra29586.444040164206...
SamanthaCarney25476.145540174271...
Missing Account ID

But the other table has account ID information as well and this other table has missing account ID too. So can we combine account ID such that we have full list of Account ID with non missing information?

Yes, this is possible.

with this code:

proc sql;
select FirstName, LastName, Income,
coalesce(c.AccountID,t.AccountID) as AccountID format=10.,
DateTime, MerchantID, Amount
from sq.smallcustomer as c full join
sq.smalltransaction as t
on c.AccountID = t.AccountID;
quit;
See? we still have one missing information but that is because in both the tables that information was not available.

Until next time, if you want to learn SAS by an expert and become a Quant developer, reach out to us at [email protected]

Keep Learning and Take Care <3

By RichS

One thought on “Coalesce SAS Function”

Leave a Reply

Your email address will not be published. Required fields are marked *


Fatal error: Uncaught wfWAFStorageFileException: Unable to save temporary file for atomic writing. in /home/beasjrbr/quantfinanc.com/wp-content/plugins/wordfence/vendor/wordfence/wf-waf/src/lib/storage/file.php:34 Stack trace: #0 /home/beasjrbr/quantfinanc.com/wp-content/plugins/wordfence/vendor/wordfence/wf-waf/src/lib/storage/file.php(658): wfWAFStorageFile::atomicFilePutContents('/home/beasjrbr/...', '<?php exit('Acc...') #1 [internal function]: wfWAFStorageFile->saveConfig('livewaf') #2 {main} thrown in /home/beasjrbr/quantfinanc.com/wp-content/plugins/wordfence/vendor/wordfence/wf-waf/src/lib/storage/file.php on line 34