<?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: check if macro variable is null in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/check-if-macro-variable-is-null/m-p/114518#M23591</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Macro parameters always exist in the local scope symbol table for the macro.&amp;nbsp; That is why SYMEXIST will not work for macro parameters.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The answer depends on what you mean by empty.&amp;nbsp; Personally for macro parameters I usually just test using %LENGTH(&amp;amp;mvar).&amp;nbsp; This works great for macro parameters as there is no need to worry about macro quoting as any special characters would have to already have been quoted by the user when they called the macro.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you want to treat a macro parameter that only contains spaces as also being empty then you need a more robust method.&amp;nbsp; In that case use the method from SUGI the paper that has been sited above in this thread.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 23 Jan 2015 15:03:27 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2015-01-23T15:03:27Z</dc:date>
    <item>
      <title>check if macro variable is null</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-if-macro-variable-is-null/m-p/114506#M23579</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have macro x with optional parameters var1, var2.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I call eg. %x(A,B) , %x(,B)&lt;/P&gt;&lt;P&gt;I need to set default value in case eg. var1 is null. To do this I need reliable method to check wether var1 is null or not.&lt;/P&gt;&lt;P&gt;I tried %SYMEXIST(var1) but apparently all macro variables are initialized but with some kind of null value.&lt;/P&gt;&lt;P&gt;I tried %length(var1) =0 but sas says the lenght is &amp;gt;0&lt;/P&gt;&lt;P&gt;Should I use some trim or other command to get rid of some spaces of other "invisible" characters?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks for help&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 14 May 2012 16:35:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-if-macro-variable-is-null/m-p/114506#M23579</guid>
      <dc:creator>tom12122</dc:creator>
      <dc:date>2012-05-14T16:35:23Z</dc:date>
    </item>
    <item>
      <title>Re: check if macro variable is null</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-if-macro-variable-is-null/m-p/114507#M23580</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* macro to test macro parameters */
/* returns 1 if the tested parameter is blank */
/* 0 otherwise, blank means all characters are,*/
/* or are macro variables that resolve to a  */
/* blank */
/* param can be up to 65,531 characters long */
/* if numeric and several 1000 digits long may*/
/* hang the session. (Windows 32 bit OS) */
/* NOT a test for a NULL (zero length string) */
/* though may work for some of those as well */
&amp;nbsp;
%macro isBlank(param);
  %sysevalf(%superq(param)=,boolean)
%mend isBlank;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller﻿&lt;/a&gt;&amp;nbsp;also adds:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;To see if macro variable is null, Chang CHung and John King examined several different solutions and came to a clear winner&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://support.sas.com/resources/papers/proceedings09/022-2009.pdf" target="_self"&gt;http://support.sas.com/resources/papers/proceeding&lt;WBR /&gt;s09/022-2009.pdf&lt;/A&gt;&lt;/P&gt;
&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Aug 2016 13:06:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-if-macro-variable-is-null/m-p/114507#M23580</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-08-03T13:06:33Z</dc:date>
    </item>
    <item>
      <title>Re: check if macro variable is null</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-if-macro-variable-is-null/m-p/114508#M23581</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You can use the macro logic %if &amp;amp;b= %then to check for a null value.&lt;/P&gt;&lt;P&gt;See the example below.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%let a=test;&lt;/P&gt;&lt;P&gt;%let b=;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%macro test(a, b);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; %if &amp;amp;a = %then %let a=blank;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; %if &amp;amp;b = %then %let b=bblank;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; %put &amp;amp;a. &amp;amp;b.;&lt;/P&gt;&lt;P&gt;%mend;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%test(&amp;amp;a, &amp;amp;b);&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 14 May 2012 16:59:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-if-macro-variable-is-null/m-p/114508#M23581</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2012-05-14T16:59:20Z</dc:date>
    </item>
    <item>
      <title>Re: check if macro variable is null</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-if-macro-variable-is-null/m-p/114509#M23582</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;If you've ever seen a macro that uses a two-letter state code, you know this can be a tricky problem when STATE=OR.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here's the simple way:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%if %length(&amp;amp;a)=0 %then %do;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Good luck.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 14 May 2012 18:07:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-if-macro-variable-is-null/m-p/114509#M23582</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2012-05-14T18:07:53Z</dc:date>
    </item>
    <item>
      <title>Re: check if macro variable is null</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-if-macro-variable-is-null/m-p/114510#M23583</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I know it can cause issues but odds are OR would be another variable or in quotes so it's usually unlikely.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 14 May 2012 18:12:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-if-macro-variable-is-null/m-p/114510#M23583</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2012-05-14T18:12:47Z</dc:date>
    </item>
    <item>
      <title>Re: check if macro variable is null</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-if-macro-variable-is-null/m-p/114511#M23584</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Since you said you are already using %LENGTH, check for quoting.&amp;nbsp; For example, this would get you a length of 3:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%let var = %str(&amp;nbsp;&amp;nbsp; );&lt;/P&gt;&lt;P&gt;%let len = %length(&amp;amp;var);&lt;/P&gt;&lt;P&gt;%put LEN is &amp;amp;len..;&lt;/P&gt;&lt;P&gt;If that's not it, then examine more closely what is in your macro variable&amp;nbsp;&amp;nbsp; Try a DATA step approach:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data _null_;&lt;/P&gt;&lt;P&gt;hex_code = put("&amp;amp;var1", $hex12.);&lt;/P&gt;&lt;P&gt;put "&amp;amp;var1&amp;nbsp; "&amp;nbsp; hex_code=;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 14 May 2012 20:48:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-if-macro-variable-is-null/m-p/114511#M23584</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2012-05-14T20:48:18Z</dc:date>
    </item>
    <item>
      <title>Re: check if macro variable is null</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-if-macro-variable-is-null/m-p/114512#M23585</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;did you forget the &amp;amp; before var1?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 16 May 2012 14:01:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-if-macro-variable-is-null/m-p/114512#M23585</guid>
      <dc:creator>Jay_TxOAG</dc:creator>
      <dc:date>2012-05-16T14:01:35Z</dc:date>
    </item>
    <item>
      <title>Re: check if macro variable is null</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-if-macro-variable-is-null/m-p/114513#M23586</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I like to use %if &amp;amp;var1=%str() %then.... just to keep from wondering why there isn't anything after the =&lt;/P&gt;&lt;P&gt;but I have run into problems with quoting with this method...I have started to use %length now...do you find that %length works almost all the time?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 16 May 2012 14:05:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-if-macro-variable-is-null/m-p/114513#M23586</guid>
      <dc:creator>Jay_TxOAG</dc:creator>
      <dc:date>2012-05-16T14:05:20Z</dc:date>
    </item>
    <item>
      <title>Re: check if macro variable is null</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-if-macro-variable-is-null/m-p/114514#M23587</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Jay,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Yes, I find that %LENGTH works almost all of the time.&amp;nbsp; It can fail if the incoming macro variable contains unbalanced parentheses, although it seems to work if the macro variable contains a semicolon.&amp;nbsp; I haven't tested it for all other special characters.&amp;nbsp; I'm also experimenting with another variation: &lt;/P&gt;&lt;P&gt;%length(%superq(var1))&lt;/P&gt;&lt;P&gt;%LENGTH avoids trouble in a few situations.&amp;nbsp; I already mentioned that the incoming macro variable might take on a keyword value such as OR or NE.&amp;nbsp; Another situation is where &amp;amp;VAR1 contains a path name that includes an arithmetic trigger.&amp;nbsp; This generates an error:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%if /path/to/folder = %then %do;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;When macro language sees "/" as part of an %IF condition, it assumes it should apply the %EVAL function.&amp;nbsp; Not a good decision in this case.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The %superq version works in more cases, but generates interesting questions.&amp;nbsp; What should happen if &amp;amp;VAR1 is created by CALL SYMPUT and contains a few blanks?&amp;nbsp; Should &amp;amp;VAR1 be considered to be null or not?&amp;nbsp; Adding %superq changes the results.&amp;nbsp; And what should happen if CALL SYMPUT assigned &amp;amp;VAR2 as the value of &amp;amp;VAR1.&amp;nbsp; Should &amp;amp;VAR1 be null when &amp;amp;VAR2 is null? &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 16 May 2012 14:41:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-if-macro-variable-is-null/m-p/114514#M23587</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2012-05-16T14:41:49Z</dc:date>
    </item>
    <item>
      <title>Re: check if macro variable is null</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-if-macro-variable-is-null/m-p/114515#M23588</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;To see if macro variable is null, Chang CHung and John King examined several different solutions and came to a clear winner&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A class="unlinked" title="http://support.sas.com/resources/papers/proceedings09/022-2009.pd"&gt;http://support.sas.com/resources/papers/proceedings09/022-2009.pdf&lt;/A&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 16 May 2012 15:29:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-if-macro-variable-is-null/m-p/114515#M23588</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2012-05-16T15:29:53Z</dc:date>
    </item>
    <item>
      <title>Re: check if macro variable is null</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-if-macro-variable-is-null/m-p/114516#M23589</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;@Astounding - Thanks for the explanation&lt;/P&gt;&lt;P&gt;@PaigeMiller - Thanks for the reference...I couldn't find it at that link, but I found it Here...&lt;A href="http://changchung.com/download/022-2009.pdf" title="http://changchung.com/download/022-2009.pdf"&gt;http://changchung.com/download/022-2009.pdf&lt;/A&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 16 May 2012 16:40:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-if-macro-variable-is-null/m-p/114516#M23589</guid>
      <dc:creator>Jay_TxOAG</dc:creator>
      <dc:date>2012-05-16T16:40:09Z</dc:date>
    </item>
    <item>
      <title>Re: check if macro variable is null</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-if-macro-variable-is-null/m-p/114517#M23590</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks, Paige&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This document was just the thing I needed!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Tom&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 23 Jan 2015 11:44:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-if-macro-variable-is-null/m-p/114517#M23590</guid>
      <dc:creator>TomKari</dc:creator>
      <dc:date>2015-01-23T11:44:21Z</dc:date>
    </item>
    <item>
      <title>Re: check if macro variable is null</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-if-macro-variable-is-null/m-p/114518#M23591</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Macro parameters always exist in the local scope symbol table for the macro.&amp;nbsp; That is why SYMEXIST will not work for macro parameters.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The answer depends on what you mean by empty.&amp;nbsp; Personally for macro parameters I usually just test using %LENGTH(&amp;amp;mvar).&amp;nbsp; This works great for macro parameters as there is no need to worry about macro quoting as any special characters would have to already have been quoted by the user when they called the macro.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you want to treat a macro parameter that only contains spaces as also being empty then you need a more robust method.&amp;nbsp; In that case use the method from SUGI the paper that has been sited above in this thread.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 23 Jan 2015 15:03:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-if-macro-variable-is-null/m-p/114518#M23591</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2015-01-23T15:03:27Z</dc:date>
    </item>
    <item>
      <title>Re: check if macro variable is null</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-if-macro-variable-is-null/m-p/114519#M23592</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;And yet in the paper by Chung and King, the test using %length(&amp;amp;mvar) has a drawback and performed worse than the winner found in that paper.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 23 Jan 2015 15:33:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-if-macro-variable-is-null/m-p/114519#M23592</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2015-01-23T15:33:26Z</dc:date>
    </item>
    <item>
      <title>Re: check if macro variable is null</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-if-macro-variable-is-null/m-p/114520#M23593</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Because that paper has a different definition of NULL and is for testing general macro variables rather than just macro parameters.&lt;/P&gt;&lt;P&gt;Personally to me if someone goes to the effort to use macro quoting in their macro call to pass in a value with non-zero length that is NOT a null parameter value.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 23 Jan 2015 15:41:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-if-macro-variable-is-null/m-p/114520#M23593</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2015-01-23T15:41:33Z</dc:date>
    </item>
  </channel>
</rss>

