<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to handle missing macro variables with Coalescec function in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-handle-missing-macro-variables-with-Coalescec-function/m-p/629789#M186339</link>
    <description>&lt;P&gt;I ran as below to handle both the variables but it is not working as excepted. Am I missing something?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 05 Mar 2020 14:06:36 GMT</pubDate>
    <dc:creator>David_Billa</dc:creator>
    <dc:date>2020-03-05T14:06:36Z</dc:date>
    <item>
      <title>How to handle missing macro variables with Coalescec function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-handle-missing-macro-variables-with-Coalescec-function/m-p/629773#M186329</link>
      <description>&lt;P&gt;In the following code, I want the macro variable _SOURCE_CD to resolve it as missing. Also I don't wish explicitly create that macro variable&amp;nbsp;with missing before I call this macro variable. Could you please help me to resolve this?&amp;nbsp;&lt;LI-WRAPPER&gt;&lt;/LI-WRAPPER&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let _LEGAL_CD=RMN;

data l;
lel=compress(COALESCEC("&amp;amp;_SOURCE_CD.","&amp;amp;_LEGAL_CD."));
call symput('lel',lel);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;EM&gt;&lt;STRONG&gt;Log:&lt;/STRONG&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;26         %let _LEGAL_CD=RMN;
27         
28         
29         data l;
30         lel=compress(COALESCEC("&amp;amp;_SOURCE_CD.","&amp;amp;_LEGAL_CD."));
WARNING: Apparent symbolic reference _SOURCE_CD not resolved.
SYMBOLGEN:  Macro variable _LEGAL_CD resolves to RMN
31         call symput('lel',lel);
32         run;

NOTE: The data set WORK.L has 1 observations and 1 variables.&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;U&gt;Output:&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="131"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="131"&gt;&lt;STRONG&gt;lel&lt;/STRONG&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&amp;amp;_SOURCE_CD.&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;Desired Output:&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="131"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="131"&gt;&lt;STRONG&gt;lel&lt;/STRONG&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;RMN&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Thu, 05 Mar 2020 11:58:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-handle-missing-macro-variables-with-Coalescec-function/m-p/629773#M186329</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2020-03-05T11:58:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle missing macro variables with Coalescec function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-handle-missing-macro-variables-with-Coalescec-function/m-p/629774#M186330</link>
      <description>&lt;P&gt;Interesting, looks like a bug - or strange feature. I can't see a reason why coalescec returns the name of the unresolvable variable, instead of the value of _Legal_CD. If parameter positions in coalescec are switched, LEL is set&amp;nbsp; as expected. I can't think of a beautiful way to solve this.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Mar 2020 12:27:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-handle-missing-macro-variables-with-Coalescec-function/m-p/629774#M186330</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-03-05T12:27:15Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle missing macro variables with Coalescec function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-handle-missing-macro-variables-with-Coalescec-function/m-p/629776#M186332</link>
      <description>&lt;P&gt;In order to use a macro variable, it MUST be defined.&lt;/P&gt;
&lt;P&gt;So you need to set it to empty at least:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let _SOURCE_CD=;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 05 Mar 2020 12:31:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-handle-missing-macro-variables-with-Coalescec-function/m-p/629776#M186332</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-03-05T12:31:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle missing macro variables with Coalescec function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-handle-missing-macro-variables-with-Coalescec-function/m-p/629777#M186333</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Interesting, looks like a bug - or strange feature. I can't see a reason why coalescec returns the name of the unresolvable variable, instead of the value of _Legal_CD. If parameter positions in coalescec are switched, LEL is set&amp;nbsp; as expected. I can't think of a beautiful way to solve this.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That's because coalescec() does not see a variable, it sees text. The macro processor tries to resolve the macro variable, does not find it, and so throws a WARNING and lets the text stay as it is. And so the data step compiler compiles a non-empty string literal for the coalescec() function, and since at execution time the function now gets a non-missing value as its first argument, it returns that. This is very clearly not a bug, just a mistaken use of macro variables.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Mar 2020 12:40:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-handle-missing-macro-variables-with-Coalescec-function/m-p/629777#M186333</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-03-05T12:40:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle missing macro variables with Coalescec function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-handle-missing-macro-variables-with-Coalescec-function/m-p/629784#M186335</link>
      <description>&lt;P&gt;Just expanding my question. If the macro variables which was in the Coalescec function not exist then I want to put the value as missing so that I can get the desired value in my Output.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is this possible?&lt;/P&gt;</description>
      <pubDate>Thu, 05 Mar 2020 13:14:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-handle-missing-macro-variables-with-Coalescec-function/m-p/629784#M186335</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2020-03-05T13:14:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle missing macro variables with Coalescec function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-handle-missing-macro-variables-with-Coalescec-function/m-p/629785#M186336</link>
      <description>&lt;P&gt;You need to make use of the SYMEXIST and SYMGET functions, and use a temporary variable to get a completely clean log:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let _LEGAL_CD=RMN;


data l;
if symexist("_SOURCE_CD")
then _source_cd = symget("_SOURCE_CD");
else _source_cd = '';
lel=compress(COALESCEC(_source_cd,"&amp;amp;_LEGAL_CD."));
call symput('lel',lel);
drop _:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 05 Mar 2020 13:22:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-handle-missing-macro-variables-with-Coalescec-function/m-p/629785#M186336</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-03-05T13:22:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle missing macro variables with Coalescec function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-handle-missing-macro-variables-with-Coalescec-function/m-p/629786#M186337</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can declare the macrovariable to ensure that it exists in all contexts.&lt;/P&gt;
&lt;P&gt;If it has already be defined, the declaration will not change its value and if&lt;/P&gt;
&lt;P&gt;not it will be defined as a space character, which is SAS way to define missing&lt;/P&gt;
&lt;P&gt;character variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How you declare a macrovariable without setting its value depends on the context.&lt;/P&gt;
&lt;P&gt;"%global" gives the macrovariable a global scope and "%local", in a macro, limits&lt;/P&gt;
&lt;P&gt;the sccope to the macro body. Redeclaring as global a macrovariable which has already&lt;/P&gt;
&lt;P&gt;been defined in the local context results in an error.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;1    %let _LEGAL_CD=RMN;
2
3    %global _SOURCE_CD;
4
5    data l;
6    lel=compress(COALESCEC("&amp;amp;_SOURCE_CD.","&amp;amp;_LEGAL_CD."));
7    call symput('lel',lel);
8    run;

NOTE: The data set WORK.L has 1 observations and 1 variables.
NOTE: DATA statement used (Total process time):
      real time           4.09 seconds
      cpu time            0.00 seconds


9
10   %put &amp;amp;=lel.;
LEL=RMN

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 05 Mar 2020 13:25:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-handle-missing-macro-variables-with-Coalescec-function/m-p/629786#M186337</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2020-03-05T13:25:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle missing macro variables with Coalescec function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-handle-missing-macro-variables-with-Coalescec-function/m-p/629789#M186339</link>
      <description>&lt;P&gt;I ran as below to handle both the variables but it is not working as excepted. Am I missing something?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Mar 2020 14:06:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-handle-missing-macro-variables-with-Coalescec-function/m-p/629789#M186339</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2020-03-05T14:06:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle missing macro variables with Coalescec function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-handle-missing-macro-variables-with-Coalescec-function/m-p/629791#M186341</link>
      <description>&lt;P&gt;You accidentally commented your second %let.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Mar 2020 13:36:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-handle-missing-macro-variables-with-Coalescec-function/m-p/629791#M186341</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-03-05T13:36:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle missing macro variables with Coalescec function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-handle-missing-macro-variables-with-Coalescec-function/m-p/629793#M186343</link>
      <description>&lt;P&gt;Yes,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You retrieve macrovariables values in a SAS dataset columns but you still use the macrovariables&lt;/P&gt;
&lt;P&gt;in your coalesce call instead of the dataset variables.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Mar 2020 13:38:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-handle-missing-macro-variables-with-Coalescec-function/m-p/629793#M186343</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2020-03-05T13:38:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle missing macro variables with Coalescec function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-handle-missing-macro-variables-with-Coalescec-function/m-p/629794#M186344</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt; thanks for correcting the gibberish i wrote ... &lt;/P&gt;</description>
      <pubDate>Thu, 05 Mar 2020 13:40:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-handle-missing-macro-variables-with-Coalescec-function/m-p/629794#M186344</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-03-05T13:40:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle missing macro variables with Coalescec function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-handle-missing-macro-variables-with-Coalescec-function/m-p/629795#M186345</link>
      <description>&lt;P&gt;No, I purposely commended the second let Statement to see how the code behaves. If you the code, you can see the changes which I made from your Version.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Mar 2020 13:42:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-handle-missing-macro-variables-with-Coalescec-function/m-p/629795#M186345</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2020-03-05T13:42:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle missing macro variables with Coalescec function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-handle-missing-macro-variables-with-Coalescec-function/m-p/629797#M186347</link>
      <description>&lt;P&gt;As&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/30622"&gt;@gamotte&lt;/a&gt;&amp;nbsp;mentioned, you omitted using the data step variables in the coalescec function.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Mar 2020 13:45:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-handle-missing-macro-variables-with-Coalescec-function/m-p/629797#M186347</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-03-05T13:45:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle missing macro variables with Coalescec function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-handle-missing-macro-variables-with-Coalescec-function/m-p/629798#M186348</link>
      <description>&lt;P&gt;Could you please help me correct it?&lt;/P&gt;</description>
      <pubDate>Thu, 05 Mar 2020 13:47:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-handle-missing-macro-variables-with-Coalescec-function/m-p/629798#M186348</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2020-03-05T13:47:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle missing macro variables with Coalescec function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-handle-missing-macro-variables-with-Coalescec-function/m-p/629801#M186350</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data l;
if symexist("_SOURCE_SYS_CD")
then _SOURCE_SYS_CD = symget("_SOURCE_SYS_CD");
else _SOURCE_SYS_CD = '';
if symexist("_LEGAL_ENTITY_CD")
then _LEGAL_ENTITY_CD = symget("_LEGAL_ENTITY_CD");
else _LEGAL_ENTITY_CD = '';
le=compress(COALESCEC(_SOURCE_SYS_CD,_LEGAL_ENTITY_CD));
call symput('le',le);
drop _:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 05 Mar 2020 13:52:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-handle-missing-macro-variables-with-Coalescec-function/m-p/629801#M186350</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-03-05T13:52:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle missing macro variables with Coalescec function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-handle-missing-macro-variables-with-Coalescec-function/m-p/629811#M186353</link>
      <description>&lt;P&gt;Edited per&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/30622"&gt;@gamotte&lt;/a&gt;'s reminder of macro statement %SYMEXIST, thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I wouldn't use DATA step for this, since you're not processing data.&amp;nbsp; You can just do your COASLESCE with an %IF statement, e.g.:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if %symexist(_SOURCE_SYS_CD) %then %do ;
  %let lel=&amp;amp;_Source_SYS_CD ;
%end ;
%else %do ;
  %let lel=&amp;amp;_LEGAL_CD ;
%end ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Mar 2020 14:19:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-handle-missing-macro-variables-with-Coalescec-function/m-p/629811#M186353</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2020-03-05T14:19:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle missing macro variables with Coalescec function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-handle-missing-macro-variables-with-Coalescec-function/m-p/629815#M186354</link>
      <description>&lt;P&gt;Note that sysfunc is not necessary here as there exists a macro %symexist equivalent.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Mar 2020 14:17:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-handle-missing-macro-variables-with-Coalescec-function/m-p/629815#M186354</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2020-03-05T14:17:46Z</dc:date>
    </item>
  </channel>
</rss>

