<?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 whats wrong with this varexist function ? in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/whats-wrong-with-this-varexist-function/m-p/944141#M45242</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to test the varexist function but I don't get the expected result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can someone Help&amp;nbsp; me with that issue&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;
%mend varexist;
%let fname=class;
%let var=Name;

%macro test;

data test;
set sashelp.class;
%if %varexist(&amp;amp;fname.,&amp;amp;var.) eq 1 %then rename Name=Name1;
run;

%mend test;
%test;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 16 Sep 2024 19:27:35 GMT</pubDate>
    <dc:creator>alepage</dc:creator>
    <dc:date>2024-09-16T19:27:35Z</dc:date>
    <item>
      <title>whats wrong with this varexist function ?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/whats-wrong-with-this-varexist-function/m-p/944141#M45242</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to test the varexist function but I don't get the expected result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can someone Help&amp;nbsp; me with that issue&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;
%mend varexist;
%let fname=class;
%let var=Name;

%macro test;

data test;
set sashelp.class;
%if %varexist(&amp;amp;fname.,&amp;amp;var.) eq 1 %then rename Name=Name1;
run;

%mend test;
%test;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 16 Sep 2024 19:27:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/whats-wrong-with-this-varexist-function/m-p/944141#M45242</guid>
      <dc:creator>alepage</dc:creator>
      <dc:date>2024-09-16T19:27:35Z</dc:date>
    </item>
    <item>
      <title>Re: whats wrong with this varexist function ?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/whats-wrong-with-this-varexist-function/m-p/944144#M45244</link>
      <description>&lt;P&gt;You don't have a data set named CLASS.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, you are missing a semi-colon, it should be&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if %varexist(&amp;amp;fname.,&amp;amp;var.) eq 1 %then rename Name=Name1%str(;);
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 16 Sep 2024 19:40:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/whats-wrong-with-this-varexist-function/m-p/944144#M45244</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-09-16T19:40:49Z</dc:date>
    </item>
    <item>
      <title>Re: whats wrong with this varexist function ?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/whats-wrong-with-this-varexist-function/m-p/944151#M45247</link>
      <description>&lt;P&gt;how will you do that in a proc sql ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;						proc sql;
                					create table table_info as
									%if %varexist(a,'police') eq 1 %then rename police=agreement_nbr%str(;);
                					select distinct a.agreement_nbr as agreement_nbr length=20, 
							      	a.lob as lob length=4,
                         			a.month as month length=3,
							      	a,year as year length=4,
                         			a.filename as filename length=25, 
                         			a.path as libname length=100,
									a.policy_expiry_date length=8,
									b.*
                		from work.&amp;amp;fname. as a
						inner join lookup as b
						on(a.agreement_nbr=b.agreement_nbr_dr and a.province_cd=b.province and a.company_cd=b.legacy_underwriting_company);
						
                		quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Please note that is some dataset we have the variable police that need to be rename Agreement_nbr and in other datasets we already have agreement_nbr.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 16 Sep 2024 20:14:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/whats-wrong-with-this-varexist-function/m-p/944151#M45247</guid>
      <dc:creator>alepage</dc:creator>
      <dc:date>2024-09-16T20:14:46Z</dc:date>
    </item>
    <item>
      <title>Re: whats wrong with this varexist function ?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/whats-wrong-with-this-varexist-function/m-p/944152#M45248</link>
      <description>&lt;P&gt;You are missing an ELSE branch for when the dataset does not exist (or cannot be opened).&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;Let's try it out:&lt;/P&gt;
&lt;PRE&gt;34   %put Does NAME exist in SASHELP.CLASS? %varexist(var=NAME,ds=SASHELP.CLASS) ;
Does NAME exist in SASHELP.CLASS? 1
35   data class;
36     set sashelp.class;
37   run;

NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.CLASS has 19 observations and 5 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.01 seconds


38   %put Does NAME exist in CLASS? %varexist(class,name) ;
Does NAME exist in CLASS? 1
39   proc delete data=class; run;

NOTE: Deleting WORK.CLASS (memtype=DATA).
NOTE: PROCEDURE DELETE used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


40   %put Does NAME exist in CLASS? %varexist(class,name) ;
Does NAME exist in CLASS? 0

&lt;/PRE&gt;
&lt;P&gt;For the full version look here:&amp;nbsp;&lt;A href="https://github.com/sasutils/macros/blob/master/varexist.sas" target="_blank"&gt;https://github.com/sasutils/macros/blob/master/varexist.sas&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 16 Sep 2024 20:15:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/whats-wrong-with-this-varexist-function/m-p/944152#M45248</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-09-16T20:15:29Z</dc:date>
    </item>
    <item>
      <title>Re: whats wrong with this varexist function ?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/whats-wrong-with-this-varexist-function/m-p/944161#M45249</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76331"&gt;@alepage&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;how will you do that in a proc sql ?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I didn't give an answer for SQL because your question clearly is using a DATA step.&lt;/P&gt;</description>
      <pubDate>Mon, 16 Sep 2024 21:21:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/whats-wrong-with-this-varexist-function/m-p/944161#M45249</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-09-16T21:21:41Z</dc:date>
    </item>
    <item>
      <title>Re: whats wrong with this varexist function ?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/whats-wrong-with-this-varexist-function/m-p/944168#M45250</link>
      <description>&lt;P&gt;I really doubt that you have a variable named&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;'police'&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;First you would have to have set the VALIDVARNAME option to ANY to allow SAS to even use a variable name with quotes in it.&amp;nbsp; Second you would need to use a name literal like "'police'"n in the SAS code to reference it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the goal is to use one of two different names in the SQL code I find it much easier to first create a macro variable with the correct name to use, rather than trying to insert %IF and other macro logic into the middle of actual SAS statements.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also the macro code is easier to type and easier to read if you just treat the result of %VAREXIST() call as a BOOLEAN value, instead of thinking of it as having numeric values that you need to test for equality to something.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if %varexist(&amp;amp;fname,police) %then %let varname=police;
%else %if %varexist(&amp;amp;fname,agreement_nbr) %then %let varname=agreement_nbr;


proc sql;
create table table_info as
  select 
    a.&amp;amp;varname as agreement_nbr length=20
  , a.lob as lob length=4
  , a.month as month length=3
  , a.year as year length=4
  , a.filename as filename length=25
  , a.path as libname length=100
  , a.policy_expiry_date length=8
  , b.*
  from &amp;amp;fname. as a
  inner join lookup as b
    on a.&amp;amp;varname=b.agreement_nbr_dr
    and a.province_cd=b.province
    and a.company_cd=b.legacy_underwriting_company
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Do you really have YEAR stored in a character variable?&amp;nbsp; If it is a numeric variable why would you bother telling SAS to store only 4 of the 8 bytes needed to represent a number?&amp;nbsp; You normally never need to tell SAS to use a storage length other than 8 for numeric variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PS Your TAB key seems to be messing up.&amp;nbsp; You had random tabs inserted into your code which makes the code appear very jagged on the left.&amp;nbsp; I find it works much better if you change the SAS editor settings to replace tab characters with actual spaces.&amp;nbsp; That way it is much harder to make such a mess.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1726527584078.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/100398iE6F0163711A32B1B/image-size/large?v=v2&amp;amp;px=999" role="button" title="Tom_0-1726527584078.png" alt="Tom_0-1726527584078.png" /&gt;&lt;/span&gt;&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, 17 Sep 2024 02:07:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/whats-wrong-with-this-varexist-function/m-p/944168#M45250</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-09-17T02:07:14Z</dc:date>
    </item>
  </channel>
</rss>

