<?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: macro variable varname not resolve in a call execute in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/macro-variable-varname-not-resolve-in-a-call-execute/m-p/944635#M45255</link>
    <description>&lt;P&gt;I have declare the varname and varname2 as global.&amp;nbsp; Then I don't have any warning.&amp;nbsp; But I still have the issue with varname which is always blank.&lt;/P&gt;</description>
    <pubDate>Thu, 19 Sep 2024 18:18:21 GMT</pubDate>
    <dc:creator>alepage</dc:creator>
    <dc:date>2024-09-19T18:18:21Z</dc:date>
    <item>
      <title>macro variable varname not resolve in a call execute</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/macro-variable-varname-not-resolve-in-a-call-execute/m-p/944626#M45252</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I trying to use if and else if condition to define macro variables varname and varname2 but I am unable to check their value.&lt;/P&gt;
&lt;P&gt;Does someone can help me with that issue.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
   set inforcelisting5;
   call execute
   (compbl(cat(
   "Libname dssource ", engine,'"',path,'";',
   '%nrstr(%macro lookuptbl;)',
   '%nrstr(%if %varexist(',strip(fname),',police) %then %do; %let varname=police;%end;)',
   '%nrstr(%else %if %varexist(',strip(fname),',agreement_nbr) %then %do; %let varname=agreement_nbr;%end;)',
   '%nrstr(%else %if ',strip(cie),' eq be %then %do; %let varname2=datechea;%end;)',
   '%nrstr(%mend lookuptbl;)',
   '%nrstr(%lookuptbl;)',
	'%nrstr(%put &amp;amp;=varname &amp;amp;=varname2;)', 
   "Libname dssource clear;"
)));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;partial log file:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;NOTE: CALL EXECUTE generated line.&lt;BR /&gt;1 + Libname dssource base"/dwh_actuariat/sasdata/sas2002/be/habi/bel.prod1000.sah1stat.dec2002.data/ ";&lt;BR /&gt;NOTE: Libref DSSOURCE was successfully assigned as follows: &lt;BR /&gt;Engine: BASE &lt;BR /&gt;Physical Name: /dwh_actuariat/sasdata/sas2002/be/habi/bel.prod1000.sah1stat.dec2002.data/&lt;BR /&gt;1 + %macro lookuptbl;%if &lt;BR /&gt;%varexist(polices,police) %then %do; %let varname=police;%end;%else %if %varexist(polices,agreement_nbr) %then %do; %let&lt;BR /&gt;2 The SAS System 13:22 Thursday, September 19, 2024&lt;/P&gt;
&lt;P&gt;2 + varname=agreement_nbr;%end;%else %if be eq be %then %do; %let varname2=datechea;%end;%mend lookuptbl;%lookuptbl;%put &lt;BR /&gt;&amp;amp;=varname &amp;amp;=varname2;Libname dssource clear;&lt;BR /&gt;WARNING: Apparent symbolic reference VARNAME not resolved.&lt;BR /&gt;WARNING: Apparent symbolic reference VARNAME2 not resolved.&lt;BR /&gt;varname varname2&lt;BR /&gt;NOTE: Libref DSSOURCE has been deassigned.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Sep 2024 17:31:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/macro-variable-varname-not-resolve-in-a-call-execute/m-p/944626#M45252</guid>
      <dc:creator>alepage</dc:creator>
      <dc:date>2024-09-19T17:31:27Z</dc:date>
    </item>
    <item>
      <title>Re: macro variable varname not resolve in a call execute</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/macro-variable-varname-not-resolve-in-a-call-execute/m-p/944629#M45253</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;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I trying to use if and else if condition to define macro variables varname and varname2 but I am unable to check their value.&lt;/P&gt;
&lt;P&gt;Does someone can help me with that issue.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
   set inforcelisting5;
   call execute
   (compbl(cat(
   "Libname dssource ", engine,'"',path,'";',
   '%nrstr(%macro lookuptbl;)',
   '%nrstr(%if %varexist(',strip(fname),',police) %then %do; %let varname=police;%end;)',
   '%nrstr(%else %if %varexist(',strip(fname),',agreement_nbr) %then %do; %let varname=agreement_nbr;%end;)',
   '%nrstr(%else %if ',strip(cie),' eq be %then %do; %let varname2=datechea;%end;)',
   '%nrstr(%mend lookuptbl;)',
   '%nrstr(%lookuptbl;)',
	'%nrstr(%put &amp;amp;=varname &amp;amp;=varname2;)', 
   "Libname dssource clear;"
)));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;partial log file:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;NOTE: CALL EXECUTE generated line.&lt;BR /&gt;1 + Libname dssource base"/dwh_actuariat/sasdata/sas2002/be/habi/bel.prod1000.sah1stat.dec2002.data/ ";&lt;BR /&gt;NOTE: Libref DSSOURCE was successfully assigned as follows: &lt;BR /&gt;Engine: BASE &lt;BR /&gt;Physical Name: /dwh_actuariat/sasdata/sas2002/be/habi/bel.prod1000.sah1stat.dec2002.data/&lt;BR /&gt;1 + %macro lookuptbl;%if &lt;BR /&gt;%varexist(polices,police) %then %do; %let varname=police;%end;%else %if %varexist(polices,agreement_nbr) %then %do; %let&lt;BR /&gt;2 The SAS System 13:22 Thursday, September 19, 2024&lt;/P&gt;
&lt;P&gt;2 + varname=agreement_nbr;%end;%else %if be eq be %then %do; %let varname2=datechea;%end;%mend lookuptbl;%lookuptbl;%put &lt;BR /&gt;&amp;amp;=varname &amp;amp;=varname2;Libname dssource clear;&lt;BR /&gt;WARNING: Apparent symbolic reference VARNAME not resolved.&lt;BR /&gt;WARNING: Apparent symbolic reference VARNAME2 not resolved.&lt;BR /&gt;varname varname2&lt;BR /&gt;NOTE: Libref DSSOURCE has been deassigned.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;After cleaning up the log text so it looks a bit more like nice code I get this:&lt;/P&gt;
&lt;PRE&gt;%macro lookuptbl;
   %if %varexist(polices,police) %then %do; 
      %let varname=police;
   %end;
   %else %if %varexist(polices,agreement_nbr) %then %do; 
      %let varname=agreement_nbr;
   %end;
   %else %if be eq be %then %do; 
     %let varname2=datechea;
   %end;
%mend lookuptbl;
%lookuptbl;
%put &amp;amp;=varname &amp;amp;=varname2;
Libname dssource clear;&lt;/PRE&gt;
&lt;P&gt;Which tells me the reason the variables do not exist where you use the %put at the end because they are currently only LOCAL in scope to the macro %lookuptbl.&lt;/P&gt;
&lt;P&gt;You can fix this by adding something inside the definition of the macro that generates:&lt;/P&gt;
&lt;PRE&gt;%global varname varname2 ;&lt;/PRE&gt;
&lt;P&gt;I really don't see much reason for this macro definition to be in call execute statements though.&lt;/P&gt;
&lt;P&gt;Perhaps provide some PARAMETERS to the macro definition instead of recompiling it with different values of Fname and Cie in the body of the macro.&lt;/P&gt;
&lt;P&gt;Maybe something like&lt;/P&gt;
&lt;PRE&gt;%macro lookuptbl (val1=, val2=;
   %if %varexist(&amp;amp;val1.,police) %then %do; 
      %let varname=police;
   %end;
   %else %if %varexist(&amp;amp;val2.,agreement_nbr) %then %do; 
      %let varname=agreement_nbr;
   %end;
   %else %if be eq be %then %do; 
     %let varname2=datechea;
   %end;
%mend lookuptbl;&lt;/PRE&gt;
&lt;P&gt;Then you would just have to call the macro with the val1=fname, val2=cie .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you are naming the library Dssource because you have hard coded the library name into whatever that %varexist macro might be you have also likely made a poor style choice for that macro.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Sep 2024 19:00:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/macro-variable-varname-not-resolve-in-a-call-execute/m-p/944629#M45253</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-09-19T19:00:53Z</dc:date>
    </item>
    <item>
      <title>Re: macro variable varname not resolve in a call execute</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/macro-variable-varname-not-resolve-in-a-call-execute/m-p/944633#M45254</link>
      <description>&lt;P&gt;You are defining the macro LOOKUPTBL to set values into macro variables named VARNAME and VARNAME2, but there is no code that defines VARNAME.&amp;nbsp; So when LOOKUPTBL runs it will make VARNAME as LOCAL to LOOKUPTBL.&amp;nbsp; Which means when LOOKUPTBL ends they will no longer exist.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;You could simple add a couple of %LET statements before your data step&lt;/STRONG&gt; to make sure those macro variables already exist when LOOKUPTBL runs so that the %LET statements will update the values of those macro variables.&amp;nbsp; Then your %PUT after LOOKUPTBL has ended will be able to find them.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But why are you using a data step to DEFINE the macro?&amp;nbsp; Just design&amp;nbsp;the macro to except whatever it is that changes as a normal input.&amp;nbsp; It looks like that is the value of the FNAME dataset variable.&amp;nbsp; So perhaps define it with a macro parameter.&amp;nbsp; Either call it FNAME also or perhaps DATA or DSNAME.&amp;nbsp; Then in the data step just CALL the macro passing in the value of FNAME.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro lookuptbl(fname,cie);
%if not %symexist(varname) %then %global varname;
%if not %symexist(varname2) %then %global varname2;
%if %varexist(&amp;amp;fname,police) %then %let varname=police;
%else %if %varexist(&amp;amp;fname,agreement_nbr) %then %let varname=agreement_nbr;
%else %if (&amp;amp;cie = 'be') %then %let varname2=datechea;
%mend lookuptbl;

data _null_;
   set inforcelisting5;
   call execute
   (catx(' ','libname dssource',engine,quote(trim(path),"'"),';'
   ,'%nrstr(%lookuptbl)(fname=',fname,',cie=',quote(trim(cie),"'"),');'
   ,'%nrstr(%put &amp;amp;=varname &amp;amp;=varname2;)'
   ,'libname dssource clear;'
   ));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or perhaps pass in the values of ENGINE and PATH also and have the macro do more and the data _null_ step do less.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Sep 2024 18:14:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/macro-variable-varname-not-resolve-in-a-call-execute/m-p/944633#M45254</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-09-19T18:14:20Z</dc:date>
    </item>
    <item>
      <title>Re: macro variable varname not resolve in a call execute</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/macro-variable-varname-not-resolve-in-a-call-execute/m-p/944635#M45255</link>
      <description>&lt;P&gt;I have declare the varname and varname2 as global.&amp;nbsp; Then I don't have any warning.&amp;nbsp; But I still have the issue with varname which is always blank.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Sep 2024 18:18:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/macro-variable-varname-not-resolve-in-a-call-execute/m-p/944635#M45255</guid>
      <dc:creator>alepage</dc:creator>
      <dc:date>2024-09-19T18:18:21Z</dc:date>
    </item>
    <item>
      <title>Re: macro variable varname not resolve in a call execute</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/macro-variable-varname-not-resolve-in-a-call-execute/m-p/944653#M45256</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;I have declare the varname and varname2 as global.&amp;nbsp; Then I don't have any warning.&amp;nbsp; But I still have the issue with varname which is always blank.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;From the logic of your macro that means that none of the conditions were met.&lt;/P&gt;
&lt;P&gt;Both of the two variables you looked for with %VAREXIST() were not found and the CIE value was not be.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Run it first without all of the complications and make sure it works.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First find the value of FNAME and ENGINE and PATH and CIE you used from your dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then use them to generate the LIBNAME statement and the two %VAREXIST macro calls.&lt;/P&gt;
&lt;P&gt;For example if ENGINE as empty and PATH as /home/myname/mydir and the value of FNAME as dssource.fred then run code like&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;libname dssource '/home/myname/mydir';
%put %varexist(dssource.fred,police);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Notice that for your logic to make any sense the value of FNAME in the dataset has to include the libref DSSOURCE that you defined with the LIBNAME statement.&amp;nbsp; If the value of FNAME was just FRED then your call to %VAREXIST() would need to include the dssource. before the value of FNAME.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also you can run %IF/%THEN logic in open code (without defining a macro) as long as you use %DO/%END blocks and do not attempt to nest them.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Sep 2024 19:09:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/macro-variable-varname-not-resolve-in-a-call-execute/m-p/944653#M45256</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-09-19T19:09:46Z</dc:date>
    </item>
  </channel>
</rss>

