<?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 case when else end as  varname is not working properly when the variable does not exist in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/case-when-else-end-as-varname-is-not-working-properly-when-the/m-p/944951#M45274</link>
    <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to create&amp;nbsp; new variable using a case when else end as company_cd but it does not work as 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;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
   set inforcelisting5;
   call execute
   (compbl(cat(
   "Libname dssource ", engine,'"',path,'";',
   '%nrstr(%macro lookuptbl;)',
   /******************** Mapping few variables *************************************/
   '%nrstr(%global varname1 varname2 varname3 ;)',
   '%nrstr(%if %varexist(dssource.',strip(fname),',police) %then %let varname1=police;)',
   '%nrstr(%else %if %varexist(dssource.',strip(fname),',agreement_nbr) %then %let varname1=agreement_nbr;)',
   '%nrstr(%if %varexist(dssource.',strip(fname),',police) %then %let varname2=datechea;)',
   '%nrstr(%else %if %varexist(dssource.',strip(fname),',agreement_nbr) %then %let varname2=policy_expiry_dt;)',
   '%nrstr(%if %varexist(dssource.',strip(fname),',province) %then %let varname3=province;)',
   '%nrstr(%else %if %varexist(dssource.',strip(fname),',risk_province_cd) %then %let varname3=risk_province_cd;)',
   '%nrstr(%else %if %varexist(dssource.',strip(fname),',policy_province_cd) %then %let varname3=policy_province_cd;)',
   '%nrstr(%else %if %varexist(dssource.',strip(fname),',province_cd) %then %let varname3=province_cd;)',   
   '%nrstr(%put &amp;amp;=varname1 &amp;amp;=varname2 &amp;amp;=varname3;)', 
   /************************ End of Mapping few variables ***************************/
   "proc sql;",
   "create table table_info as ",
   '%nrstr(Select distinct strip(a.&amp;amp;varname1.) as agreement_nbr length=20 )',
   ',"',strip(cie), '"  as cie length=2',
   ',"',strip(lob), '"  as lob length=4',
   ',"',strip(year), '"  as year length=4',
   ',"',strip(fname), '"  as filename length=25',
   ',"',strip(path), '"  as libname length=100 ',   
   '%nrstr(, CASE)',
   '%nrstr(     WHEN "',strip(cie),'" eq "be" then "B" )',
   '%nrstr(     WHEN "',strip(cie),'" eq "gc" then "A" )',
   '%nrstr(     WHEN "',strip(cie),'" eq "gp" then "P" )',
   '%nrstr(     WHEN "',strip(cie),'" not in ("be","gc","gp") then a.company_cd )',
   '%nrstr( END AS COMPANY_CD )',
   '%nrstr(from dssource.',strip(fname),' as  a ; )', 
    "Quit;",
   '%nrstr(%mend lookuptbl;)',
   '%nrstr(%lookuptbl;)',	
   "Libname dssource clear;"
)));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;proc sql;&lt;/P&gt;
&lt;P&gt;select&lt;/P&gt;
&lt;P&gt;case&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;when cie eq 'be' then "B"&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;when cie eq 'gc' then "A"&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; When cie eq 'gp' then "P"&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; when cie not in ('be','gc','gp') then a.company_cd&lt;/P&gt;
&lt;P&gt;end as company_cd&lt;/P&gt;
&lt;P&gt;from ...&lt;/P&gt;
&lt;P&gt;innner join...&lt;/P&gt;
&lt;P&gt;on()...&lt;/P&gt;
&lt;P&gt;Quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This script should map company_cd properly it does work properly.&amp;nbsp; Does someone has an idea how to solve that issue&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 &lt;BR /&gt;lookuptbl;%global varname1 varname2 varname3 ;%if %varexist(dssource.polices,police) %then %let varname1=police;%else %if %varexist&lt;BR /&gt;2 +(dssource.polices,agreement_nbr) %then %let varname1=agreement_nbr;%if %varexist(dssource.polices,police) %then %let &lt;BR /&gt;varname2=datechea;%else %if %varexist(dssource.polices,agreement_nbr) %then %let varname2=policy_expiry_dt;%if %varexist&lt;BR /&gt;3 +(dssource.polices,province) %then %let varname3=province;%else %if %varexist(dssource.polices,risk_province_cd) %then &lt;BR /&gt;%let varname3=risk_province_cd;%else %if %varexist(dssource.polices,policy_province_cd) %then %let &lt;BR /&gt;varname3=policy_province_cd;%else %if&lt;BR /&gt;4 + %varexist(dssource.polices,province_cd) %then %let varname3=province_cd;%put &amp;amp;=varname1 &amp;amp;=varname2 &amp;amp;=varname3;proc &lt;BR /&gt;sql;create table table_info as Select distinct strip(a.&amp;amp;varname1.) as agreement_nbr length=20 ,"be" as cie length=2,"habi" as lob&lt;BR /&gt;5 + length=4,"2002" as year length=4,"polices" as filename &lt;BR /&gt;length=25,"/dwh_actuariat/sasdata/sas2002/be/habi/bel.prod1000.sah1stat.dec2002.data/" as libname length=100 , CASE WHEN "be" eq &lt;BR /&gt;"be" then "B" WHEN "be" eq "gc" then "A" WHEN "be" eq "gp" then "P"&lt;BR /&gt;6 + WHEN "be" not in ("be","gc","gp") then a.company_cd END AS COMPANY_CD from dssource.polices as a ; Quit;%mend &lt;BR /&gt;lookuptbl;%lookuptbl;Libname dssource clear;&lt;BR /&gt;VARNAME1=police VARNAME2=datechea VARNAME3=province&lt;BR /&gt;ERROR: Column company_cd could not be found in the table/view identified with the correlation name A.&lt;BR /&gt;NOTE: A CASE expression has no ELSE clause. Cases not accounted for by the WHEN clauses will result in a missing value for the CASE &lt;BR /&gt;expression.&lt;BR /&gt;NOTE: PROC SQL set option NOEXEC and will continue to check the syntax of statements.&lt;BR /&gt;NOTE: The SAS System stopped processing this step because of errors.&lt;BR /&gt;NOTE: PROCEDURE SQL used (Total process time):&lt;BR /&gt;real time 0.00 seconds&lt;BR /&gt;cpu time 0.01 seconds&lt;BR /&gt;&lt;BR /&gt;NOTE: Libref DSSOURCE has been deassigned.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 23 Sep 2024 21:12:01 GMT</pubDate>
    <dc:creator>alepage</dc:creator>
    <dc:date>2024-09-23T21:12:01Z</dc:date>
    <item>
      <title>case when else end as  varname is not working properly when the variable does not exist</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/case-when-else-end-as-varname-is-not-working-properly-when-the/m-p/944951#M45274</link>
      <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to create&amp;nbsp; new variable using a case when else end as company_cd but it does not work as 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;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
   set inforcelisting5;
   call execute
   (compbl(cat(
   "Libname dssource ", engine,'"',path,'";',
   '%nrstr(%macro lookuptbl;)',
   /******************** Mapping few variables *************************************/
   '%nrstr(%global varname1 varname2 varname3 ;)',
   '%nrstr(%if %varexist(dssource.',strip(fname),',police) %then %let varname1=police;)',
   '%nrstr(%else %if %varexist(dssource.',strip(fname),',agreement_nbr) %then %let varname1=agreement_nbr;)',
   '%nrstr(%if %varexist(dssource.',strip(fname),',police) %then %let varname2=datechea;)',
   '%nrstr(%else %if %varexist(dssource.',strip(fname),',agreement_nbr) %then %let varname2=policy_expiry_dt;)',
   '%nrstr(%if %varexist(dssource.',strip(fname),',province) %then %let varname3=province;)',
   '%nrstr(%else %if %varexist(dssource.',strip(fname),',risk_province_cd) %then %let varname3=risk_province_cd;)',
   '%nrstr(%else %if %varexist(dssource.',strip(fname),',policy_province_cd) %then %let varname3=policy_province_cd;)',
   '%nrstr(%else %if %varexist(dssource.',strip(fname),',province_cd) %then %let varname3=province_cd;)',   
   '%nrstr(%put &amp;amp;=varname1 &amp;amp;=varname2 &amp;amp;=varname3;)', 
   /************************ End of Mapping few variables ***************************/
   "proc sql;",
   "create table table_info as ",
   '%nrstr(Select distinct strip(a.&amp;amp;varname1.) as agreement_nbr length=20 )',
   ',"',strip(cie), '"  as cie length=2',
   ',"',strip(lob), '"  as lob length=4',
   ',"',strip(year), '"  as year length=4',
   ',"',strip(fname), '"  as filename length=25',
   ',"',strip(path), '"  as libname length=100 ',   
   '%nrstr(, CASE)',
   '%nrstr(     WHEN "',strip(cie),'" eq "be" then "B" )',
   '%nrstr(     WHEN "',strip(cie),'" eq "gc" then "A" )',
   '%nrstr(     WHEN "',strip(cie),'" eq "gp" then "P" )',
   '%nrstr(     WHEN "',strip(cie),'" not in ("be","gc","gp") then a.company_cd )',
   '%nrstr( END AS COMPANY_CD )',
   '%nrstr(from dssource.',strip(fname),' as  a ; )', 
    "Quit;",
   '%nrstr(%mend lookuptbl;)',
   '%nrstr(%lookuptbl;)',	
   "Libname dssource clear;"
)));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;proc sql;&lt;/P&gt;
&lt;P&gt;select&lt;/P&gt;
&lt;P&gt;case&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;when cie eq 'be' then "B"&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;when cie eq 'gc' then "A"&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; When cie eq 'gp' then "P"&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; when cie not in ('be','gc','gp') then a.company_cd&lt;/P&gt;
&lt;P&gt;end as company_cd&lt;/P&gt;
&lt;P&gt;from ...&lt;/P&gt;
&lt;P&gt;innner join...&lt;/P&gt;
&lt;P&gt;on()...&lt;/P&gt;
&lt;P&gt;Quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This script should map company_cd properly it does work properly.&amp;nbsp; Does someone has an idea how to solve that issue&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 &lt;BR /&gt;lookuptbl;%global varname1 varname2 varname3 ;%if %varexist(dssource.polices,police) %then %let varname1=police;%else %if %varexist&lt;BR /&gt;2 +(dssource.polices,agreement_nbr) %then %let varname1=agreement_nbr;%if %varexist(dssource.polices,police) %then %let &lt;BR /&gt;varname2=datechea;%else %if %varexist(dssource.polices,agreement_nbr) %then %let varname2=policy_expiry_dt;%if %varexist&lt;BR /&gt;3 +(dssource.polices,province) %then %let varname3=province;%else %if %varexist(dssource.polices,risk_province_cd) %then &lt;BR /&gt;%let varname3=risk_province_cd;%else %if %varexist(dssource.polices,policy_province_cd) %then %let &lt;BR /&gt;varname3=policy_province_cd;%else %if&lt;BR /&gt;4 + %varexist(dssource.polices,province_cd) %then %let varname3=province_cd;%put &amp;amp;=varname1 &amp;amp;=varname2 &amp;amp;=varname3;proc &lt;BR /&gt;sql;create table table_info as Select distinct strip(a.&amp;amp;varname1.) as agreement_nbr length=20 ,"be" as cie length=2,"habi" as lob&lt;BR /&gt;5 + length=4,"2002" as year length=4,"polices" as filename &lt;BR /&gt;length=25,"/dwh_actuariat/sasdata/sas2002/be/habi/bel.prod1000.sah1stat.dec2002.data/" as libname length=100 , CASE WHEN "be" eq &lt;BR /&gt;"be" then "B" WHEN "be" eq "gc" then "A" WHEN "be" eq "gp" then "P"&lt;BR /&gt;6 + WHEN "be" not in ("be","gc","gp") then a.company_cd END AS COMPANY_CD from dssource.polices as a ; Quit;%mend &lt;BR /&gt;lookuptbl;%lookuptbl;Libname dssource clear;&lt;BR /&gt;VARNAME1=police VARNAME2=datechea VARNAME3=province&lt;BR /&gt;ERROR: Column company_cd could not be found in the table/view identified with the correlation name A.&lt;BR /&gt;NOTE: A CASE expression has no ELSE clause. Cases not accounted for by the WHEN clauses will result in a missing value for the CASE &lt;BR /&gt;expression.&lt;BR /&gt;NOTE: PROC SQL set option NOEXEC and will continue to check the syntax of statements.&lt;BR /&gt;NOTE: The SAS System stopped processing this step because of errors.&lt;BR /&gt;NOTE: PROCEDURE SQL used (Total process time):&lt;BR /&gt;real time 0.00 seconds&lt;BR /&gt;cpu time 0.01 seconds&lt;BR /&gt;&lt;BR /&gt;NOTE: Libref DSSOURCE has been deassigned.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 23 Sep 2024 21:12:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/case-when-else-end-as-varname-is-not-working-properly-when-the/m-p/944951#M45274</guid>
      <dc:creator>alepage</dc:creator>
      <dc:date>2024-09-23T21:12:01Z</dc:date>
    </item>
    <item>
      <title>Re: case when else end as  varname is not working properly when the variable does not exist</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/case-when-else-end-as-varname-is-not-working-properly-when-the/m-p/944952#M45275</link>
      <description>&lt;P&gt;Test the value of CIE in the DATA STEP, not in the SQL code.&lt;/P&gt;
&lt;P&gt;Use the data step to only generate the SQL code that includes A.COMPANY_CD when it exists.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 23 Sep 2024 21:17:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/case-when-else-end-as-varname-is-not-working-properly-when-the/m-p/944952#M45275</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-09-23T21:17:53Z</dc:date>
    </item>
    <item>
      <title>Re: case when else end as  varname is not working properly when the variable does not exist</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/case-when-else-end-as-varname-is-not-working-properly-when-the/m-p/944953#M45276</link>
      <description>&lt;P&gt;I will test that latter during the evening. Thanks&lt;/P&gt;</description>
      <pubDate>Mon, 23 Sep 2024 21:23:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/case-when-else-end-as-varname-is-not-working-properly-when-the/m-p/944953#M45276</guid>
      <dc:creator>alepage</dc:creator>
      <dc:date>2024-09-23T21:23:30Z</dc:date>
    </item>
    <item>
      <title>Re: case when else end as  varname is not working properly when the variable does not exist</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/case-when-else-end-as-varname-is-not-working-properly-when-the/m-p/944961#M45277</link>
      <description>&lt;P&gt;With so many "variable does not exist" errors this is starting to sound like you really don't know your data sets.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there some terribly important reason that your aren't use an ELSE in&amp;nbsp; that CASE:&lt;/P&gt;
&lt;PRE&gt;case
     when cie eq 'be' then "B"
     when cie eq 'gc' then "A"
    When cie eq 'gp' then "P"
    else a.company_cd
end as company_cd&lt;/PRE&gt;
&lt;P&gt;Would certainly be&amp;nbsp; a lot shorter than saying "compare this value to the already compared list and if it is not in that list then do this assignment"&lt;/P&gt;</description>
      <pubDate>Mon, 23 Sep 2024 21:58:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/case-when-else-end-as-varname-is-not-working-properly-when-the/m-p/944961#M45277</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-09-23T21:58:42Z</dc:date>
    </item>
    <item>
      <title>Re: case when else end as  varname is not working properly when the variable does not exist</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/case-when-else-end-as-varname-is-not-working-properly-when-the/m-p/945069#M45279</link>
      <description>&lt;P&gt;when cie in (be, gc,gp) the variable company_cd does not exist and I want to map this variable respectively to B, A, P.&lt;/P&gt;
&lt;P&gt;When cie is not in (be,gc,gp) the variable company_cd exits and I want to use this value.&lt;/P&gt;</description>
      <pubDate>Tue, 24 Sep 2024 15:35:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/case-when-else-end-as-varname-is-not-working-properly-when-the/m-p/945069#M45279</guid>
      <dc:creator>alepage</dc:creator>
      <dc:date>2024-09-24T15:35:09Z</dc:date>
    </item>
    <item>
      <title>Re: case when else end as  varname is not working properly when the variable does not exist</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/case-when-else-end-as-varname-is-not-working-properly-when-the/m-p/945075#M45280</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;when cie in (be, gc,gp) the variable company_cd does not exist and I want to map this variable respectively to B, A, P.&lt;/P&gt;
&lt;P&gt;When cie is not in (be,gc,gp) the variable company_cd exits and I want to use this value.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That appears not to be what you wrote in the code and is why you get&lt;/P&gt;
&lt;P&gt;ERROR: Column company_cd could not be found in the table/view identified with the correlation name A.&lt;/P&gt;
&lt;P&gt;You cannot use a reference to a variable if it does not exist. Period.&lt;/P&gt;
&lt;P&gt;So you need something OUTSIDE of the Proc SQL that is checking the value of cie and when it is one of those value you cannot have the CODE with that a.company_cd name referenced.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, describe what that SQL step is supposed to actually do. In some detail.&lt;/P&gt;
&lt;P&gt;WHEN clauses assume the variable exists. It appears that is not the actual case from that comment so that SQl really needs a clear cut description of what it is supposed to do. Just like the rest of this code.&lt;/P&gt;
&lt;P&gt;Or better, what is the whole process supposed to do and what does that source data set contain.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I still don't see any answer to why you though CALL EXECUTE and making multiple versions of the macro was needed in the first place.&lt;/P&gt;</description>
      <pubDate>Tue, 24 Sep 2024 16:58:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/case-when-else-end-as-varname-is-not-working-properly-when-the/m-p/945075#M45280</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-09-24T16:58:58Z</dc:date>
    </item>
    <item>
      <title>Re: case when else end as  varname is not working properly when the variable does not exist</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/case-when-else-end-as-varname-is-not-working-properly-when-the/m-p/945076#M45281</link>
      <description>&lt;P&gt;This looks like an unusually complex approach.&amp;nbsp; I enjoy the macro language, and CALL EXECUTE, but I think it's rare to use CALL EXECUTE to execute the code to define a macro. In this case, when the DATA step runs it will generate code to create the macro LOOKUPTBL.&amp;nbsp; The macro itself has no&amp;nbsp; parameters but in a sense it's dynamic because you're generating the macro source code dynamically.&amp;nbsp; If there are 10 records in work.inforcelisting5 you'll create the macro LOOKUPTBL 10 times, and each time it could have a different definition.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would think this would be easier to design, debug, and maintain if you just define a macro with a few parameters:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro lookuptbl (data=,varname1=, varname2=, varname3=);
  ...   
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;With that approach you could still use CALL EXECUTE to generate the macro calls, and you could use if VAREXIST to decide which variable names to pass into each macro call.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or if you want to leave the variable exists tests in the macro, you could do it like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro lookuptbl(data);

%local varname1 varname2 varname3;

%if %varexist(&amp;amp;data,police) %then %let varname1=police;
%else %if %varexist(&amp;amp;data,agreement_nbr) %then %let varname1=agreement_nbr;
*...;

%put &amp;amp;=varname1 &amp;amp;=varname2 &amp;amp;=varname3;
   
proc sql;
  create table table_info as
  select distinct
    strip(a.&amp;amp;varname1.) as agreement_nbr length=20
   ,strip(cie) as cie length=2',
   /* ... */
  ;
quit;

%mend lookuptbl;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The idea of dynamically generating macro definitions is kind of scary to me, but I'm sure people do it. Usually macros are dynamic enough, especially when paired with using CALL EXECUTE or similar to generate macro calls.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 24 Sep 2024 17:17:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/case-when-else-end-as-varname-is-not-working-properly-when-the/m-p/945076#M45281</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2024-09-24T17:17:37Z</dc:date>
    </item>
  </channel>
</rss>

