<?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: help determining whether a variable exists in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/help-determining-whether-a-variable-exists/m-p/726865#M225932</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88384"&gt;@Shmuel&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;%GLOBAL clause is just a declaration and will not override existing value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;By the way, I checked myself and length("&amp;amp;none"), when NONE was not assigned a value,&lt;/P&gt;
&lt;P&gt;result in 1 and not 0 as I expected.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Blindly adding a %GLOBAL statement inside a macro can cause errors if the macro variable already exists in a currently running macro's local symbol table.&amp;nbsp; Use the %SYMEXIST() to test for this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In a data step use the LENGTHN() function to return zero when the string only contains blanks.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In macro code use the %LENGTH() function to test if a macro variable contains anything (even blanks).&lt;/P&gt;
&lt;P&gt;Or use the test described in this classic paper to check if the macro variable non-blank.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://support.sas.com/resources/papers/proceedings09/022-2009.pdf" target="_blank" rel="noopener"&gt;https://support.sas.com/resources/papers/proceedings09/022-2009.pdf&lt;/A&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Will lengthn() or %length() work if the variable doesn't exist which is the case now?&lt;/P&gt;</description>
    <pubDate>Tue, 16 Mar 2021 18:56:06 GMT</pubDate>
    <dc:creator>DanD999</dc:creator>
    <dc:date>2021-03-16T18:56:06Z</dc:date>
    <item>
      <title>help determining whether a variable exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/help-determining-whether-a-variable-exists/m-p/726809#M225906</link>
      <description>&lt;P&gt;I'm working with a code module that is called by other code modules. Some of the other code modules that call this common module with have a certain variable set and other code modules won't.&lt;/P&gt;
&lt;P&gt;I've tried various code but either I get an error or the code doesn't recognize whether the variable exists or not.&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;%let choice = yes;
%put cd9 &amp;amp;choice.; 

data have;
input ID Comments:$10. Grade;
cards;
1 abcdefg 2
1 lkjhgff 3
1 poiuytr 1
10 mnbvcxa 1
10 mbdfrte 4
10 qwertyu 2
;
run;

data result;
	set have;
	if %sysfunc(exist(&amp;amp;choice.)) then asgn=catx(' ',comments, &amp;amp;choice.);
	else asgn=catx(' ', comments, ' missing');
run;
data result;
	set have;
	if %symexist(&amp;amp;choice.) then asgn=catx(' ',comments, &amp;amp;choice.);
	else asgn=catx(' ', comments, ' missing');
run;

data result;
	set have;
	if exist(&amp;amp;choice.) then asgn=catx(' ',comments, &amp;amp;choice.);
	else asgn=catx(' ', comments, ' missing');
run;
data result;
	set have;
	if exist(&amp;amp;choice1.) then asgn=catx(' ',comments, &amp;amp;choice1.);
	else asgn=catx(' ', comments, ' missing');
run;&lt;/LI-CODE&gt;
&lt;P&gt;Thanks for any help&amp;nbsp;or guidance you can give.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Mar 2021 15:38:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/help-determining-whether-a-variable-exists/m-p/726809#M225906</guid>
      <dc:creator>DanD999</dc:creator>
      <dc:date>2021-03-16T15:38:34Z</dc:date>
    </item>
    <item>
      <title>Re: help determining whether a variable exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/help-determining-whether-a-variable-exists/m-p/726813#M225907</link>
      <description>&lt;P&gt;There are a couple of logic issues in your code.&amp;nbsp; First the EXISTS() function is for datasets, not variables.&amp;nbsp; Second you cannot test for existence of a variable using datastep logic in the same dataset.&amp;nbsp; Once you reference a variable in your code the compiler of the data step will see the reference and so the variable will exist in the data step (even if it doesn't exist in the source dataset).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In most cases it doesn't matter whether the variable exists or not.&amp;nbsp; &amp;nbsp;Just make sure to define it before referencing it in your code so that you don't accidentally create the wrong type of variable when it doesn't exist.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you really do need to make different code depending on whether the variable exists or not then you must &lt;A href="https://github.com/sasutils/macros/blob/master/varexist.sas" target="_self"&gt;test for its existence first&lt;/A&gt; and then conditionally generate the code using macro logic based on the results of your test.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
%if %varexist(have,&amp;amp;choice) %then %do;
  * code that references existing variable ;
%end;
%else %do;
  * code that works when variable does not exist ;
%end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 16 Mar 2021 15:53:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/help-determining-whether-a-variable-exists/m-p/726813#M225907</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-03-16T15:53:14Z</dc:date>
    </item>
    <item>
      <title>Re: help determining whether a variable exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/help-determining-whether-a-variable-exists/m-p/726816#M225909</link>
      <description>&lt;P&gt;1)&amp;nbsp; Using function &lt;STRONG&gt;EXIST(&amp;lt;xxx&amp;gt;) -&amp;nbsp;&lt;/STRONG&gt;xxx is a &lt;U&gt;data set name&lt;/U&gt;, not a variable name !&lt;/P&gt;
&lt;P&gt;2) You assigned:&amp;nbsp;&amp;nbsp;&lt;STRONG&gt;%let choice = yes;&lt;/STRONG&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;Then in your code&amp;nbsp;&lt;STRONG&gt;&amp;amp;choise&lt;/STRONG&gt; is replaced by the string&amp;nbsp;&lt;STRONG&gt;yes&lt;/STRONG&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;Do you mean that the variable to name is&amp;nbsp;&lt;STRONG&gt;yes&lt;/STRONG&gt; ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you need to check one variable only? is it always the same variable?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or maybe you need different variables to check depending on the situation?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can check whether a variable exists in a data set by any of next methods:&lt;/P&gt;
&lt;P&gt;1) check existence of the variable in sashelp.vcolumn :&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let varname = NAME;
%let exist = 0;
data _null_;
  set sashelp.vcolumnn(where=(libname="ANYLIBRARY" and
       member = "DATA_SET_NAME");
       if _N_then call symput('exist','1');
run;
%put EXIST = &amp;amp;exist;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;pay attention = libname and data set name should be in unppercase.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2) Next code may be used inside your data step code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ds_out;
   set any_lib.ds_in;  
        ...
        dsid = open("any_lib.ds_in");
        if dsid &amp;gt; 0 then do;
            exist = varnum(dsid,"var_name");  
            dsid = close(dsid);
        end;
       if exist = 0 then put "VAR_NAME does not exist in lib.ds_in";
       else do;
              ... any code ....
       end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 16 Mar 2021 16:07:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/help-determining-whether-a-variable-exists/m-p/726816#M225909</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2021-03-16T16:07:30Z</dc:date>
    </item>
    <item>
      <title>Re: help determining whether a variable exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/help-determining-whether-a-variable-exists/m-p/726833#M225914</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;There are a couple of logic issues in your code.&amp;nbsp; First the EXISTS() function is for datasets, not variables.&amp;nbsp; Second you cannot test for existence of a variable using datastep logic in the same dataset.&amp;nbsp; Once you reference a variable in your code the compiler of the data step will see the reference and so the variable will exist in the data step (even if it doesn't exist in the source dataset).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In most cases it doesn't matter whether the variable exists or not.&amp;nbsp; &amp;nbsp;Just make sure to define it before referencing it in your code so that you don't accidentally create the wrong type of variable when it doesn't exist.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you really do need to make different code depending on whether the variable exists or not then you must &lt;A href="https://github.com/sasutils/macros/blob/master/varexist.sas" target="_self"&gt;test for its existence first&lt;/A&gt; and then conditionally generate the code using macro logic based on the results of your test.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
%if %varexist(have,&amp;amp;choice) %then %do;
  * code that references existing variable ;
%end;
%else %do;
  * code that works when variable does not exist ;
%end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I'm not sure that I understood everything you said. The variable in question is a global variable and not in a dataset. Any number of other code modules will set the variable when they start and then they call a common code module. However, the are some code modules that will not set the variable when they start and therefor I need to know if the variable exists or not. If it doesn't exist then I'll get an error the way the code is now.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So in the common code module I want to know if this variable exists ( has it been set by the calling code module). Would it be possible to test for the variable before the data step and set another variable&amp;nbsp; to Yes or No depending on whether the variable exists and then use this second variable inside of the data step?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Tue, 16 Mar 2021 17:02:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/help-determining-whether-a-variable-exists/m-p/726833#M225914</guid>
      <dc:creator>DanD999</dc:creator>
      <dc:date>2021-03-16T17:02:53Z</dc:date>
    </item>
    <item>
      <title>Re: help determining whether a variable exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/help-determining-whether-a-variable-exists/m-p/726834#M225915</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88384"&gt;@Shmuel&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;1)&amp;nbsp; Using function &lt;STRONG&gt;EXIST(&amp;lt;xxx&amp;gt;) -&amp;nbsp;&lt;/STRONG&gt;xxx is a &lt;U&gt;data set name&lt;/U&gt;, not a variable name !&lt;/P&gt;
&lt;P&gt;2) You assigned:&amp;nbsp;&amp;nbsp;&lt;STRONG&gt;%let choice = yes;&lt;/STRONG&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;Then in your code&amp;nbsp;&lt;STRONG&gt;&amp;amp;choise&lt;/STRONG&gt; is replaced by the string&amp;nbsp;&lt;STRONG&gt;yes&lt;/STRONG&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;Do you mean that the variable to name is&amp;nbsp;&lt;STRONG&gt;yes&lt;/STRONG&gt; ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is just sample code. The essence of it is can I test for the existence of a global variable and then do an if-then-else statement based on whether the global variable exists or not?&lt;/P&gt;
&lt;P&gt;Do you need to check one variable only? is it always the same variable?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or maybe you need different variables to check depending on the situation?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can check whether a variable exists in a data set by any of next methods:&lt;/P&gt;
&lt;P&gt;1) check existence of the variable in sashelp.vcolumn :&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;variable is not part of a data set . it's a global variable. also see my explanation in previous post.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let varname = NAME;
%let exist = 0;
data _null_;
  set sashelp.vcolumnn(where=(libname="ANYLIBRARY" and
       member = "DATA_SET_NAME");
       if _N_then call symput('exist','1');
run;
%put EXIST = &amp;amp;exist;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;pay attention = libname and data set name should be in unppercase.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2) Next code may be used inside your data step code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ds_out;
   set any_lib.ds_in;  
        ...
        dsid = open("any_lib.ds_in");
        if dsid &amp;gt; 0 then do;
            exist = varnum(dsid,"var_name");  
            dsid = close(dsid);
        end;
       if exist = 0 then put "VAR_NAME does not exist in lib.ds_in";
       else do;
              ... any code ....
       end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Tue, 16 Mar 2021 17:09:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/help-determining-whether-a-variable-exists/m-p/726834#M225915</guid>
      <dc:creator>DanD999</dc:creator>
      <dc:date>2021-03-16T17:09:50Z</dc:date>
    </item>
    <item>
      <title>Re: help determining whether a variable exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/help-determining-whether-a-variable-exists/m-p/726847#M225921</link>
      <description>&lt;P&gt;There is no such thing as a "global variable".&amp;nbsp; Perhaps you meant a macro variable (also know as a macro symbol)?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To test if a macro variable exists use the macro function %SYMEXIST().&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have an actual macro that you have created and you want to know if there is a global macro variable it is probably easiest to test at the start of the macro execution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example this code will check if CHOICE has been defined and if not it will create a local macro variable named CHOICE with the value of missing.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro mymacro;
%if not %symexist(choice) %then %do;
  %let choice=missing;
%end;
....
%mend ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;For a more detailed example for how to handle your situation you need to explain more about what you are doing.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example it might be that you want to check not only whether the macro variable exists but also whether or not it has been given a non-blank value.&lt;/P&gt;</description>
      <pubDate>Tue, 16 Mar 2021 18:03:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/help-determining-whether-a-variable-exists/m-p/726847#M225921</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-03-16T18:03:19Z</dc:date>
    </item>
    <item>
      <title>Re: help determining whether a variable exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/help-determining-whether-a-variable-exists/m-p/726852#M225925</link>
      <description>&lt;P&gt;Can you add to your shared model a line:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%global choice;   /* force existence of the macro variable without assigning a value */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;then you can check whether the global macro variable is assigned a value:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let chkvar = ABC;  /* this is global by default */
%global none;       /* Force existence of global variable */

%macro chk;
data _NULL_;
   if length("&amp;amp;chkvar") then put "chkvar exists";
   if not length("&amp;amp;none") then put "none do not exist";
run;
%mend;
%chk;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 16 Mar 2021 18:13:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/help-determining-whether-a-variable-exists/m-p/726852#M225925</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2021-03-16T18:13:37Z</dc:date>
    </item>
    <item>
      <title>Re: help determining whether a variable exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/help-determining-whether-a-variable-exists/m-p/726854#M225926</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88384"&gt;@Shmuel&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Can you add to your shared model a line:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%global choice;   /* force existence of the macro variable without assigning a value */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;then you can check whether the global macro variable is assigned a value:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let chkvar = ABC;  /* this is global by default */
%global none;       /* Force existence of global variable */

%macro chk;
data _NULL_;
   if length("&amp;amp;chkvar") then put "chkvar exists";
   if not length("&amp;amp;none") then put "none do not exist";
run;
%mend;
%chk;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If one of the calling code modules already has created the "choice" variable and populated it will using %Global choice in the common code module program overwrite the variable if it exists?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Tue, 16 Mar 2021 18:19:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/help-determining-whether-a-variable-exists/m-p/726854#M225926</guid>
      <dc:creator>DanD999</dc:creator>
      <dc:date>2021-03-16T18:19:09Z</dc:date>
    </item>
    <item>
      <title>Re: help determining whether a variable exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/help-determining-whether-a-variable-exists/m-p/726859#M225927</link>
      <description>&lt;P&gt;%GLOBAL clause is just a declaration and will not override existing value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;By the way, I checked myself and length("&amp;amp;none"), when NONE was not assigned a value,&lt;/P&gt;
&lt;P&gt;result in 1 and not 0 as I expected.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Mar 2021 18:34:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/help-determining-whether-a-variable-exists/m-p/726859#M225927</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2021-03-16T18:34:12Z</dc:date>
    </item>
    <item>
      <title>Re: help determining whether a variable exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/help-determining-whether-a-variable-exists/m-p/726861#M225928</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;There is no such thing as a "global variable".&amp;nbsp; Perhaps you meant a macro variable (also know as a macro symbol)?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To test if a macro variable exists use the macro function %SYMEXIST().&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have an actual macro that you have created and you want to know if there is a global macro variable it is probably easiest to test at the start of the macro execution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example this code will check if CHOICE has been defined and if not it will create a local macro variable named CHOICE with the value of missing.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro mymacro;
%if not %symexist(choice) %then %do;
  %let choice=missing;
%end;
....
%mend ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;For a more detailed example for how to handle your situation you need to explain more about what you are doing.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example it might be that you want to check not only whether the macro variable exists but also whether or not it has been given a non-blank value.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Doesn't a statement like&amp;nbsp;&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;%global choice; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;create a global variable?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have several code modules that call a code module that is common to all programs. Some of the calling programs set a variable (call it "choice") before running the common code module with an "include" statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the common code module there is a very large data step that I'm trying to change as little as possible. There is a statement in the data step that says something like "if "value a" in &amp;amp;choice. then .... else ....&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When a code module that does not set the variable "choice", calls the common code, there is an error when it gets to the line in the data step that uses the "choice" variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So I thought if I checked whether the variable exists or not from the calling program, I could do a if-then-else depending on the answer.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Tue, 16 Mar 2021 18:39:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/help-determining-whether-a-variable-exists/m-p/726861#M225928</guid>
      <dc:creator>DanD999</dc:creator>
      <dc:date>2021-03-16T18:39:03Z</dc:date>
    </item>
    <item>
      <title>Re: help determining whether a variable exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/help-determining-whether-a-variable-exists/m-p/726863#M225930</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88384"&gt;@Shmuel&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;%GLOBAL clause is just a declaration and will not override existing value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;By the way, I checked myself and length("&amp;amp;none"), when NONE was not assigned a value,&lt;/P&gt;
&lt;P&gt;result in 1 and not 0 as I expected.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Blindly adding a %GLOBAL statement inside a macro can cause errors if the macro variable already exists in a currently running macro's local symbol table.&amp;nbsp; Use the %SYMEXIST() to test for this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In a data step use the LENGTHN() function to return zero when the string only contains blanks.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In macro code use the %LENGTH() function to test if a macro variable contains anything (even blanks).&lt;/P&gt;
&lt;P&gt;Or use the test described in this classic paper to check if the macro variable non-blank.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://support.sas.com/resources/papers/proceedings09/022-2009.pdf" target="_blank"&gt;https://support.sas.com/resources/papers/proceedings09/022-2009.pdf&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Mar 2021 18:44:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/help-determining-whether-a-variable-exists/m-p/726863#M225930</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-03-16T18:44:53Z</dc:date>
    </item>
    <item>
      <title>Re: help determining whether a variable exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/help-determining-whether-a-variable-exists/m-p/726864#M225931</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/49285"&gt;@DanD999&lt;/a&gt;&amp;nbsp;I hope next code will clarify the usage of %symexist() macro function:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let x=;
%macro test;
%if %symexist(x) %then %put X; 
%else %put NOx;

%if %symexist(y) %then %put Y; 
%else %put NOy;
%mend;
%test;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;the log messages in this example:&lt;/P&gt;
&lt;PRE&gt; X
 NOy&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Mar 2021 18:50:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/help-determining-whether-a-variable-exists/m-p/726864#M225931</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2021-03-16T18:50:35Z</dc:date>
    </item>
    <item>
      <title>Re: help determining whether a variable exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/help-determining-whether-a-variable-exists/m-p/726865#M225932</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88384"&gt;@Shmuel&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;%GLOBAL clause is just a declaration and will not override existing value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;By the way, I checked myself and length("&amp;amp;none"), when NONE was not assigned a value,&lt;/P&gt;
&lt;P&gt;result in 1 and not 0 as I expected.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Blindly adding a %GLOBAL statement inside a macro can cause errors if the macro variable already exists in a currently running macro's local symbol table.&amp;nbsp; Use the %SYMEXIST() to test for this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In a data step use the LENGTHN() function to return zero when the string only contains blanks.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In macro code use the %LENGTH() function to test if a macro variable contains anything (even blanks).&lt;/P&gt;
&lt;P&gt;Or use the test described in this classic paper to check if the macro variable non-blank.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://support.sas.com/resources/papers/proceedings09/022-2009.pdf" target="_blank" rel="noopener"&gt;https://support.sas.com/resources/papers/proceedings09/022-2009.pdf&lt;/A&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Will lengthn() or %length() work if the variable doesn't exist which is the case now?&lt;/P&gt;</description>
      <pubDate>Tue, 16 Mar 2021 18:56:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/help-determining-whether-a-variable-exists/m-p/726865#M225932</guid>
      <dc:creator>DanD999</dc:creator>
      <dc:date>2021-03-16T18:56:06Z</dc:date>
    </item>
    <item>
      <title>Re: help determining whether a variable exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/help-determining-whether-a-variable-exists/m-p/726870#M225934</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;may I ask, suppose you have a macro program in a macro program:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* %let choice = xxx; -- either exists as global or does not exits */

%macro main;
   %local choise;
    ...
    %called(....);
   ...
%mend;

%macro called(...arguments ...);
    %global choice;
     %if  %symexist(choice) %then ... ;
%mend;

%main;
      &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;in such case which macro variable CHOICE shall %symexist() check - the %local or the %global ?&lt;/P&gt;</description>
      <pubDate>Tue, 16 Mar 2021 19:09:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/help-determining-whether-a-variable-exists/m-p/726870#M225934</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2021-03-16T19:09:40Z</dc:date>
    </item>
    <item>
      <title>Re: help determining whether a variable exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/help-determining-whether-a-variable-exists/m-p/726874#M225938</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88384"&gt;@Shmuel&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;may I ask, suppose you have a macro program in a macro program:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* %let choice = xxx; -- either exists as global or does not exits */

%macro main;
   %local choise;
    ...
    %called(....);
   ...
%mend;

%macro called(...arguments ...);
    %global choice;
     %if  %symexist(choice) %then ... ;
%mend;

%main;
      &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;in such case which macro variable CHOICE shall %symexist() check - the %local or the %global ?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I understand your point. Thanks for pointing it out but I don't think it will be an issue in our case. The actual name of the variable is choice_cd9. This code has been running for a long time and is rarely changed so I think that possibility is remote.&lt;/P&gt;</description>
      <pubDate>Tue, 16 Mar 2021 19:19:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/help-determining-whether-a-variable-exists/m-p/726874#M225938</guid>
      <dc:creator>DanD999</dc:creator>
      <dc:date>2021-03-16T19:19:51Z</dc:date>
    </item>
    <item>
      <title>Re: help determining whether a variable exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/help-determining-whether-a-variable-exists/m-p/726894#M225946</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88384"&gt;@Shmuel&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;may I ask, suppose you have a macro program in a macro program:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* %let choice = xxx; -- either exists as global or does not exits */

%macro main;
   %local choise;
    ...
    %called(....);
   ...
%mend;

%macro called(...arguments ...);
    %global choice;
     %if  %symexist(choice) %then ... ;
%mend;

%main;
      &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;in such case which macro variable CHOICE shall %symexist() check - the %local or the %global ?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So that is the combination will generate an error message. When the sub-macro CALLED starts and tries to create a global symbol named CHOICE it will issue an error message since CHOICE already exists in the symbol table for the MAIN macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;We are essentially trying to deal with what I call "external" macro variables.&amp;nbsp; They are not defined as parameters to the macro or defined as local macro variables that the macro uses for its own purpose.&amp;nbsp; The reference to them just seems to appear in the code and how they are supposed to have ever gotten assigned a value is magic of some type.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the general pattern I would follow.&lt;/P&gt;
&lt;P&gt;1) Test if the macro variable exists or not and then create it in the appropriate scope.&lt;/P&gt;
&lt;P&gt;2) Test if the macro variable is populated or not and if appropriate assign it a default value (or take appropriate action for when the value is empty.).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro called(...arguments ...);
    %if not %symexist(choice) %then %local choice;
    %if 0=%length(%superq(choice)) %then ... ;
     ...
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Mar 2021 20:08:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/help-determining-whether-a-variable-exists/m-p/726894#M225946</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-03-16T20:08:05Z</dc:date>
    </item>
    <item>
      <title>Re: help determining whether a variable exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/help-determining-whether-a-variable-exists/m-p/726907#M225957</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;, you absolutely clarified the point.&lt;/P&gt;</description>
      <pubDate>Tue, 16 Mar 2021 20:53:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/help-determining-whether-a-variable-exists/m-p/726907#M225957</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2021-03-16T20:53:55Z</dc:date>
    </item>
    <item>
      <title>Re: help determining whether a variable exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/help-determining-whether-a-variable-exists/m-p/728459#M226671</link>
      <description>&lt;P&gt;Thanks to everyone for their help. I wasn't able to get the code to work like I wanted so I changed other parts of the code to solve the problem.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks again&lt;/P&gt;</description>
      <pubDate>Tue, 23 Mar 2021 15:00:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/help-determining-whether-a-variable-exists/m-p/728459#M226671</guid>
      <dc:creator>DanD999</dc:creator>
      <dc:date>2021-03-23T15:00:00Z</dc:date>
    </item>
    <item>
      <title>Re: help determining whether a variable exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/help-determining-whether-a-variable-exists/m-p/897652#M354740</link>
      <description>&lt;P&gt;Hey..&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It pretty much simple.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;data check_var;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;dsname=open("sashelp.class");&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;varname_=varnum(dsname,'AGE');&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;if varname_ &amp;gt;0 then flag="Y"; else flag="N";&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;/* if flag="Y";*/&lt;/DIV&gt;&lt;DIV&gt;run;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;** If you want to create a macro then use the below code .&lt;/P&gt;&lt;P&gt;data _NULL_;&lt;BR /&gt;call symputx("Checkv", varnum(open("sashelp.class"),'AGE'));&lt;BR /&gt;run;&lt;BR /&gt;%put &amp;amp;Checkv.;&lt;/P&gt;</description>
      <pubDate>Sat, 07 Oct 2023 00:40:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/help-determining-whether-a-variable-exists/m-p/897652#M354740</guid>
      <dc:creator>Prashanth_J</dc:creator>
      <dc:date>2023-10-07T00:40:30Z</dc:date>
    </item>
  </channel>
</rss>

