<?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 know whether a variable exists in a dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-know-whether-a-variable-exists-in-a-dataset/m-p/120094#M24710</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Ron, Thanks a bunch for your reply. The second link seems to be suitable for my requirement. But, I need to check for the missing variables, and if the variable is missing, I am getting the error, and I still need to print appropriate results, and avoid the errors. I am not sure what exit macro does in the second example. I will apply this macro and see if it solves the purpose &lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://communities.sas.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 23 Feb 2013 04:55:16 GMT</pubDate>
    <dc:creator>jeeth79usa</dc:creator>
    <dc:date>2013-02-23T04:55:16Z</dc:date>
    <item>
      <title>How to know whether a variable exists in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-know-whether-a-variable-exists-in-a-dataset/m-p/120083#M24699</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello everyone,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Could you tell me if there's a SAS function (or an autocall macro) to know whether a variable exists or not in a dataset ?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The point is that I don't want to use a Datasets procedure or I/O Functions, but instead use a function (or autocall macro) this way (assuming that varexist is an autocall macro) :&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;data class;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp; set sashelp.class (keep = name&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %if %varexist(sashelp.class, weight) eq 1 %then weight;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %if %varexist(sashelp.class, xxx) eq 1 %then xxx;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; );&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Thanks for your help.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Sincerely yours,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Anthony&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 09 Jan 2013 14:25:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-know-whether-a-variable-exists-in-a-dataset/m-p/120083#M24699</guid>
      <dc:creator>Silbad</dc:creator>
      <dc:date>2013-01-09T14:25:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to know whether a variable exists in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-know-whether-a-variable-exists-in-a-dataset/m-p/120084#M24700</link>
      <description>&lt;P&gt;&lt;EM&gt;Editor's Note: This is a popular topic.&amp;nbsp; The following SAS Note also shows a&amp;nbsp;similar method for checking for the existence of a variable in a data set:&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&lt;A href="http://support.sas.com/techsup/notes/v8/25/082.html" target="_self"&gt;Sample 25082&lt;/A&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;In addition, here is a DATA Step example:&lt;/EM&gt;&lt;/P&gt;
&lt;PRE style="width: 1px; height: 1px;"&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;CODE class=" language-sas"&gt;&amp;nbsp;&lt;/CODE&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
 input fruit $ count;
 datalines;
apple 12
banana 4
coconut 5
date 7
eggs 9
fig 5
;
run;

data _null_;
 dsid=open('test');
 check=varnum(dsid,'count');
 if check=0 then put 'Variable does not exist';
 else put 'Variable is located in column ' check +(_1) '.';
run;
&lt;/CODE&gt;&lt;/PRE&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;Here is one you can use.&amp;nbsp; I have adjusted it to return 0 or 1 to match how you coded your program, but normally I just have it return the actual variable number. If you did that then you would change your code by eliminating the "eq 1" from your conditionals. SAS treats all non-zero values as TRUE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro varexist
/*----------------------------------------------------------------------
Check for the existence of a specified variable.
----------------------------------------------------------------------*/
(ds        /* Data set name */
,var       /* Variable name */);

/*----------------------------------------------------------------------
Usage Notes:

%if %varexist(&amp;amp;data,NAME)
  %then %put input data set contains variable NAME;

The macro calls resolves to 0 when either the data set does not exist
or the variable is not in the specified data set.
----------------------------------------------------------------------*/

%local dsid rc ;

/*----------------------------------------------------------------------
Use SYSFUNC to execute OPEN, VARNUM, and CLOSE functions.
-----------------------------------------------------------------------*/

%let dsid = %sysfunc(open(&amp;amp;ds));

%if (&amp;amp;dsid) %then %do;
  %if %sysfunc(varnum(&amp;amp;dsid,&amp;amp;var)) %then 1;
  %else 0 ;
  %let rc = %sysfunc(close(&amp;amp;dsid));
%end;
%else 0;

%mend varexist;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Sep 2016 17:26:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-know-whether-a-variable-exists-in-a-dataset/m-p/120084#M24700</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2016-09-28T17:26:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to know whether a variable exists in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-know-whether-a-variable-exists-in-a-dataset/m-p/120085#M24701</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;is the example below helpful:&lt;/P&gt;&lt;P&gt;%let dsn=sashelp.class;&lt;/P&gt;&lt;P&gt;%let var1=sex;&lt;/P&gt;&lt;P&gt;%let var2=abc;&lt;/P&gt;&lt;P&gt; data _null_;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; dsid=open("&amp;amp;dsn");&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; n=ifc(varnum(dsid,"&amp;amp;var1"),"&amp;amp;var1",' ');&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; m=ifc(varnum(dsid,"&amp;amp;var2"),"&amp;amp;var2",' ');&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; call symputx('vv',n);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; call symputx('ww',m);&lt;/P&gt;&lt;P&gt; run;&lt;/P&gt;&lt;P&gt; %put &amp;amp;vv &amp;amp;ww;&lt;/P&gt;&lt;P&gt; proc print data=sashelp.class;&lt;/P&gt;&lt;P&gt; var age &amp;amp;vv &amp;amp;ww;&lt;/P&gt;&lt;P&gt; run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 09 Jan 2013 14:58:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-know-whether-a-variable-exists-in-a-dataset/m-p/120085#M24701</guid>
      <dc:creator>Linlin</dc:creator>
      <dc:date>2013-01-09T14:58:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to know whether a variable exists in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-know-whether-a-variable-exists-in-a-dataset/m-p/120086#M24702</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Your answers are both good, but Tom's answer is more useful for what I'm trying to do (i.e. use a macro instead of using data steps).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you very much, Linlin and Tom ! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 09 Jan 2013 15:05:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-know-whether-a-variable-exists-in-a-dataset/m-p/120086#M24702</guid>
      <dc:creator>Silbad</dc:creator>
      <dc:date>2013-01-09T15:05:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to know whether a variable exists in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-know-whether-a-variable-exists-in-a-dataset/m-p/120087#M24703</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Tom,&lt;/P&gt;&lt;P&gt;When we use &lt;SPAN style="color: #ff0000;"&gt;open&lt;/SPAN&gt; function, do you have to use &lt;SPAN style="color: #ff0000;"&gt;close&lt;/SPAN&gt; function or it is optional?&lt;/P&gt;&lt;P&gt;Thanks - Linlin&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 09 Jan 2013 15:08:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-know-whether-a-variable-exists-in-a-dataset/m-p/120087#M24703</guid>
      <dc:creator>Linlin</dc:creator>
      <dc:date>2013-01-09T15:08:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to know whether a variable exists in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-know-whether-a-variable-exists-in-a-dataset/m-p/120088#M24704</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Unless you are planning to immediately close SAS.&amp;nbsp; Try this little test.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;data x;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;y=1;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;%let dsid=%sysfunc(open(x));&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;data x;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;y=2;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;%put %sysfunc(close(&amp;amp;dsid));&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 09 Jan 2013 15:12:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-know-whether-a-variable-exists-in-a-dataset/m-p/120088#M24704</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2013-01-09T15:12:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to know whether a variable exists in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-know-whether-a-variable-exists-in-a-dataset/m-p/120089#M24705</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&amp;nbsp; Thank you Tom!&lt;/P&gt;&lt;P&gt; So it should be fine without using close function as long as we don’t want to create same named dataset. &lt;/P&gt;&lt;P&gt;data x;&lt;BR /&gt;y=1;&lt;BR /&gt;run;&lt;BR /&gt;%let dsid=%sysfunc(open(x));&lt;/P&gt;&lt;P&gt;data new;&lt;BR /&gt;set x;&lt;BR /&gt;run;&lt;BR /&gt;proc print;run;&lt;/P&gt;&lt;P&gt;/*data x;&lt;BR /&gt;y=2;&lt;BR /&gt;run;&lt;BR /&gt;%put %sysfunc(close(&amp;amp;dsid)); */&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 09 Jan 2013 15:21:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-know-whether-a-variable-exists-in-a-dataset/m-p/120089#M24705</guid>
      <dc:creator>Linlin</dc:creator>
      <dc:date>2013-01-09T15:21:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to know whether a variable exists in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-know-whether-a-variable-exists-in-a-dataset/m-p/120090#M24706</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;If you do it thousands of time it could end up using up significant amounts of memory storing those active open file handles.&lt;/P&gt;&lt;P&gt;It also depends on the method you want to use the dataset in the future.&amp;nbsp; The dataset is open in read mode, so future read operations on the dataset are not impacted, but any operation that would lock or write to the dataset will not work.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 09 Jan 2013 15:49:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-know-whether-a-variable-exists-in-a-dataset/m-p/120090#M24706</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2013-01-09T15:49:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to know whether a variable exists in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-know-whether-a-variable-exists-in-a-dataset/m-p/120091#M24707</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you Tom! So it is a better practice to use open and close together. - Linlin&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 09 Jan 2013 16:01:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-know-whether-a-variable-exists-in-a-dataset/m-p/120091#M24707</guid>
      <dc:creator>Linlin</dc:creator>
      <dc:date>2013-01-09T16:01:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to know whether a variable exists in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-know-whether-a-variable-exists-in-a-dataset/m-p/120092#M24708</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Tom,&lt;/P&gt;&lt;P&gt;I have a similar scenario. I have a data set, and the some variables in it are&lt;/P&gt;&lt;P&gt;A1-A5&lt;/P&gt;&lt;P&gt;B1-B5&lt;/P&gt;&lt;P&gt;C1-C5&lt;/P&gt;&lt;P&gt;I would like to just check if these variables exist, and if its doesn't then read only the existing ones from the above mentioned vars.&lt;/P&gt;&lt;P&gt;I am trying to avoid the error, and this is causing the wrong results in my PDF. Would your example work in my case or I have to take different approach.&lt;/P&gt;&lt;P&gt;This is what I have so far &lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://communities.sas.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%macro varexist&lt;/P&gt;&lt;P&gt;/*----------------------------------------------------------------------&lt;/P&gt;&lt;P&gt;Check for the existence of a specified variable.&lt;/P&gt;&lt;P&gt;------------------------------------------------------------------------*/&lt;/P&gt;&lt;P&gt;(ds&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* Data set name */&lt;/P&gt;&lt;P&gt;,var&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* Variable name */&lt;/P&gt;&lt;P&gt;);&lt;/P&gt;&lt;P&gt;/*-----------------------------------------------------------------------&lt;/P&gt;&lt;P&gt;Usage Notes:&lt;/P&gt;&lt;P&gt;%if %varexist(&amp;amp;data,NAME)&lt;/P&gt;&lt;P&gt;%then %put input data set contains variable NAME;&lt;/P&gt;&lt;P&gt;The macro calls resolves to 0 when either the data set does not exist&lt;/P&gt;&lt;P&gt;or the variable is not in the specified data set.&lt;/P&gt;&lt;P&gt;----------------------------------------------------------------------*/&lt;/P&gt;&lt;P&gt;%local dsid rc ;&lt;/P&gt;&lt;P&gt;%*----------------------------------------------------------------------&lt;/P&gt;&lt;P&gt;Use SYSFUNC to execute OPEN, VARNUM, and CLOSE functions.&lt;/P&gt;&lt;P&gt;-----------------------------------------------------------------------;&lt;/P&gt;&lt;P&gt;%let dsid = %sysfunc(open(&amp;amp;ds));&lt;/P&gt;&lt;P&gt;%if (&amp;amp;dsid) %then %do;&lt;/P&gt;&lt;P&gt;&amp;nbsp; %if %sysfunc(varnum(&amp;amp;dsid,&amp;amp;var)) %then &amp;amp;var/100;&lt;/P&gt;&lt;P&gt;&amp;nbsp; %else 0 ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; %let rc = %sysfunc(close(&amp;amp;dsid));&lt;/P&gt;&lt;P&gt;%end;&lt;/P&gt;&lt;P&gt;%else 0;&lt;/P&gt;&lt;P&gt;%mend varexist;&lt;/P&gt;&lt;P&gt;%varexist(ds,A1-A5);&lt;/P&gt;&lt;P&gt;%varexist(ds,B1-B5);&lt;/P&gt;&lt;P&gt;%varexist(ds,C1-C5);&lt;/P&gt;&lt;P&gt;I was thinking to create macro variables for these vars using %let on the top.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks in Advance.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Jeeth.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 20 Feb 2013 20:11:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-know-whether-a-variable-exists-in-a-dataset/m-p/120092#M24708</guid>
      <dc:creator>jeeth79usa</dc:creator>
      <dc:date>2013-02-20T20:11:25Z</dc:date>
    </item>
    <item>
      <title>Re: How to know whether a variable exists in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-know-whether-a-variable-exists-in-a-dataset/m-p/120093#M24709</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;see also:&lt;/P&gt;&lt;P&gt;&lt;A href="http://www.sascommunity.org/wiki/Conditionally_Executing_Global_Statements" title="http://www.sascommunity.org/wiki/Conditionally_Executing_Global_Statements"&gt;http://www.sascommunity.org/wiki/Conditionally_Executing_Global_Statements&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A class="active_link" href="http://www.sascommunity.org/wiki/Macro_Assertions_for" title="http://www.sascommunity.org/wiki/Macro_Assertions_for"&gt;http://www.sascommunity.org/wiki/Macro_Assertions_for&lt;/A&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 22 Feb 2013 21:39:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-know-whether-a-variable-exists-in-a-dataset/m-p/120093#M24709</guid>
      <dc:creator>Ron_MacroMaven</dc:creator>
      <dc:date>2013-02-22T21:39:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to know whether a variable exists in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-know-whether-a-variable-exists-in-a-dataset/m-p/120094#M24710</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Ron, Thanks a bunch for your reply. The second link seems to be suitable for my requirement. But, I need to check for the missing variables, and if the variable is missing, I am getting the error, and I still need to print appropriate results, and avoid the errors. I am not sure what exit macro does in the second example. I will apply this macro and see if it solves the purpose &lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://communities.sas.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 23 Feb 2013 04:55:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-know-whether-a-variable-exists-in-a-dataset/m-p/120094#M24710</guid>
      <dc:creator>jeeth79usa</dc:creator>
      <dc:date>2013-02-23T04:55:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to know whether a variable exists in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-know-whether-a-variable-exists-in-a-dataset/m-p/120095#M24711</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;If I had the same problem as you originally stated&lt;/P&gt;&lt;P&gt;I would use Tom's macro.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Based on the way I program and my naming conventions&lt;/P&gt;&lt;P&gt;I suggest that there may be other information available to you&lt;/P&gt;&lt;P&gt;which you can use to provide a list of the variables in a data set for&lt;/P&gt;&lt;P&gt;-- in your example a keep statement --&lt;/P&gt;&lt;P&gt;some processing.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You can, for instance, use SQL to return a list of the variable in a data set into a macro variable:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="http://www.sascommunity.org/wiki/Making_Lists" title="http://www.sascommunity.org/wiki/Making_Lists"&gt;http://www.sascommunity.org/wiki/Making_Lists&lt;/A&gt;&lt;/P&gt;&lt;P&gt;%let Libname = sashelp;&lt;/P&gt;&lt;P&gt;%let Memname = class;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;PROC SQL;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; select Name&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; into :Varlist separated by ' '&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; from&amp;nbsp; Dictionary.Columns&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; where&amp;nbsp; Libname eq "%upcase(&amp;amp;Libname.)"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; and&amp;nbsp; Memname eq "%upcase(&amp;amp;Memname.)"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; and&amp;nbsp; MemType eq 'DATA';&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%Put Varlist: &amp;amp;VarList.;&lt;/P&gt;&lt;P&gt;*&lt;/P&gt;&lt;P&gt;then use the index function to check for a name in the list&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;%let indexSex = %index(&amp;amp;VarList,Sex);&lt;/P&gt;&lt;P&gt;%let existSex = %sysfunc(ifc(%index(&amp;amp;VarList,Sex)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,%nrstr(1)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,%nrstr(0)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ));&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%Let Var = Gender;&lt;/P&gt;&lt;P&gt;%let exist&amp;amp;Var = %sysfunc(ifc(%index(&amp;amp;VarList,&amp;amp;Var)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,%nrstr(1)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,%nrstr(0)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ));&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%put _user_;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;NOTE: sashelp.class var names are in Propcase.==Initial Caps&lt;/P&gt;&lt;P&gt;to ensure that this trick works&lt;/P&gt;&lt;P&gt;be sure to standardize the case to either lowcase or upcase&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Personally I prefer lowcase.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Ron Fehd&amp;nbsp; macro maven&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 25 Feb 2013 13:18:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-know-whether-a-variable-exists-in-a-dataset/m-p/120095#M24711</guid>
      <dc:creator>Ron_MacroMaven</dc:creator>
      <dc:date>2013-02-25T13:18:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to know whether a variable exists in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-know-whether-a-variable-exists-in-a-dataset/m-p/120096#M24712</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Ron,&lt;/P&gt;&lt;P&gt;Thanks for the reply. It seems to be a good trick. I am creating my variables. Once, I use this trick to check for the variables, I would like to use only the existing variables to avoid the errors in the next step. Please let me know if you can provide me a scenario for this.&lt;/P&gt;&lt;P&gt;Thanks in advance.&lt;/P&gt;&lt;P&gt;Jeeth.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 26 Feb 2013 19:59:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-know-whether-a-variable-exists-in-a-dataset/m-p/120096#M24712</guid>
      <dc:creator>jeeth79usa</dc:creator>
      <dc:date>2013-02-26T19:59:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to know whether a variable exists in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-know-whether-a-variable-exists-in-a-dataset/m-p/120097#M24713</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Compare to Tom's suggestion.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This code is lifted from Macro CallText&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A class="active_link" href="http://www.sascommunity.org/wiki/Macro_CallText" title="http://www.sascommunity.org/wiki/Macro_CallText"&gt;http://www.sascommunity.org/wiki/Macro_CallText&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt; /*&lt;/TD&gt;&lt;TD&gt;name: ...\SAS-site\macros\listvars.sas&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;description: macro function: return list of vars in data set&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;purpose&lt;/TD&gt;&lt;TD&gt;: demo SCL open, varname, and close functions&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;******/&lt;/P&gt;&lt;P&gt;%MACRO listvars&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/TD&gt;&lt;TD&gt;(Data&lt;/TD&gt;&lt;TD&gt;= sashelp.class&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/TD&gt;&lt;TD&gt;,Testing = 0&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;)/ des = 'site: macro function: return list of vars in data set'&lt;/P&gt;&lt;P&gt; /**** ** store source /* */&lt;/P&gt;&lt;P&gt;;/* RJF2 2/27/2013 2:05:28 PM&lt;/P&gt;&lt;P&gt;****** NOTE: _C_*: avoid name collisions w/data set vars ***/&lt;/P&gt;&lt;P&gt;%local _C_Col _C_Dsid _C_Hex16 _C_Nobs _C_Nvars&amp;nbsp;&amp;nbsp; _C_Rc&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&amp;nbsp;&amp;nbsp; &lt;/TD&gt;&lt;TD&gt;_C_Row _C_Type _C_Name&amp;nbsp; _C_Num&amp;nbsp; _C_Testing _C_Text&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&amp;nbsp;&amp;nbsp; &lt;/TD&gt;&lt;TD&gt;_C_Testing _C_TimeStart _C_TimeEnd;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;%let _C_Testing= %eval(&amp;nbsp;&amp;nbsp; &amp;amp;Testing&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/TD&gt;&lt;TD&gt;or(&lt;/TD&gt;&lt;TD&gt;%sysfunc(getoption(MPRINT))&amp;nbsp; eq MPRINT&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/TD&gt;&lt;TD&gt;and %sysfunc(getoption(SOURCE2)) eq SOURCE2));&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;%let _C_TimeStart = %sysfunc(datetime(),hex16.);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%**&amp;nbsp; description: assertions;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;%**&amp;nbsp; purpose&lt;/TD&gt;&lt;TD&gt;: if fail then exit;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;%if&amp;nbsp; not(%sysfunc(exist(&amp;amp;Data.))) %then %do;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt; &lt;/TD&gt;&lt;TD&gt;%put Err%str()or: &amp;amp;SysMacroname. exiting: not exist(&amp;amp;Data.);&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt; &lt;/TD&gt;&lt;TD&gt;%return;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt; &lt;/TD&gt;&lt;TD&gt;%end;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;%let _C_Dsid&amp;nbsp; = %sysfunc(open (&amp;amp;Data.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/TD&gt;&lt;TD&gt;));&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;%let _C_Nobs&amp;nbsp; = %sysfunc(attrn(&amp;amp;_C_Dsid.,Nobs ));&lt;/P&gt;&lt;P&gt;%let _C_Nvars = %sysfunc(attrn(&amp;amp;_C_Dsid.,Nvars));&lt;/P&gt;&lt;P&gt;%if&amp;nbsp; not &amp;amp;_C_Nobs. or not &amp;amp;_C_Nvars. %then %do;&lt;/P&gt;&lt;P&gt;&amp;nbsp; %put Err%str()or: &amp;amp;SysMacroName. &amp;amp;Data. obs=&amp;amp;_C_Nobs. vars=&amp;amp;_C_Nvars.;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt; &lt;/TD&gt;&lt;TD&gt;%goto CloseExit;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt; &lt;/TD&gt;&lt;TD&gt;%end;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%else&lt;/P&gt;&lt;P&gt; %put note: &amp;amp;SysMacroname. reading &amp;amp;Data. obs=&amp;amp;_C_Nobs. vars=&amp;amp;_C_Nvars.;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%do _C_Col = 1 %to &amp;amp;_C_Nvars.;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;/TD&gt;&lt;TD&gt;%sysfunc(varname (&amp;amp;_C_Dsid.,&amp;amp;_C_Col. ))&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;/TD&gt;&lt;TD&gt;%end;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;%CloseExit: %let _C_Rc = %sysfunc(close(&amp;amp;_C_Dsid.));&lt;/P&gt;&lt;P&gt;%mend listvars;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;compare with Tom's original solution for each variable.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Ron Fehd&amp;nbsp; SCL maven&lt;/P&gt;&lt;P&gt;%Let ListVars =%listvars(Data = sashelp.class);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%put ListVars:&amp;amp;ListVars.;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%Let ListVars =%listvars(Data = sashelp.heart);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%put ListVars:&amp;amp;ListVars.;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;*error: not exits data;&lt;/P&gt;&lt;P&gt;%Let ListVars =%listvars(Data = sashelp.classX);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%put ListVars:&amp;amp;ListVars.;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 27 Feb 2013 19:13:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-know-whether-a-variable-exists-in-a-dataset/m-p/120097#M24713</guid>
      <dc:creator>Ron_MacroMaven</dc:creator>
      <dc:date>2013-02-27T19:13:41Z</dc:date>
    </item>
  </channel>
</rss>

