<?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: ERROR: Column agreement_nbr could not be found in the table/view identified with the correlation in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/ERROR-Column-agreement-nbr-could-not-be-found-in-the-table-view/m-p/944775#M45260</link>
    <description>&lt;P&gt;First thing is there is NO need to redefine the macro over and over.&amp;nbsp; Just make it an test it to make sure it works.&amp;nbsp; Then your code generation step can use it with confidence.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is a simplified example (I have not idea what you intend to do with that CIE variable and how it is supposed to impact the choice of variable name to use).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro lookupname(dsname);
%if not %symexist(varname1) %then %global varname1;
%let varname1="No variable found";
%if %varexist(&amp;amp;dsname,police) %then %let varname1=police;
%else %if %varexist(&amp;amp;dsname,agreement_nbr) %then %let varname1=agreement_nbr;
%mend;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Let's try it on SASHELP.CLASS and see if it works.&lt;/P&gt;
&lt;PRE&gt;8    %lookupname(sashelp.class);
9    %put &amp;amp;=varname1;
VARNAME1="No variable found"
&lt;/PRE&gt;
&lt;P&gt;Now let's try it on a dataset that does have POLICE variable.&lt;/P&gt;
&lt;PRE&gt;11   data class;
12     set sashelp.class;
13     police=sex;
14   run;

NOTE: The data set WORK.CLASS has 19 observations and 6 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds


15
16   %lookupname(class);
17   %put &amp;amp;=varname1;
VARNAME1=police
&lt;/PRE&gt;
&lt;P&gt;I find it is &lt;STRONG&gt;MUCH easier to just use the PUT statement to generate the code to a FILE&lt;/STRONG&gt; instead of using CALL EXECUTE().&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename code temp;
data _null_;
  file code;
  set .... ;
  put ..... ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That will let you take advantage of the power of the PUT statement (and SAS formats).&amp;nbsp; You won't have to worry about the macro processor interpreting the text before it can get executed (so no more %NRSTR() functions needed).&amp;nbsp; But it also means you have something you can LOOK and make sure the logic for generating the code is working.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  infile code;
  input;
  put _infile_;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So let's try your example program.&amp;nbsp; First let's make a driver dataset that will point to the CLASS dataset we made above that does have a POLICE variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data inforcelisting5;
  length fname $32 cie $8 engine $10 path $200;
  fname='class';
  path=pathname('work');
  output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So let's just make a temporary code file.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename code temp;

data _null_;
  file code ;
  set inforcelisting5;
  put 'Libname dssource ' engine path :$quote. ';'
    / '%lookupname(dssource.' fname ');'
    / '%put &amp;amp;=varname1;'
    / 'proc sql;'
    / 'create table table_info as'
    / '  select distinct &amp;amp;varname1. as agreement_nbr length=20'
    / '  from dssource.' fname 'a'
    / ';'
    / 'quit;'
    / 'libname dssource clear;'
  ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Which will generate this code.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Libname dssource   "..." ;
%lookupname(dssource.class );
%put &amp;amp;=varname1;
proc sql;
create table table_info as
  select distinct &amp;amp;varname1. as agreement_nbr length=20
  from dssource.class a
;
quit;
libname dssource clear;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So let's run it.&lt;/P&gt;
&lt;PRE&gt;850   %include code / source2;
NOTE: %INCLUDE (level 1) file CODE is (system-specific pathname).
851  +Libname dssource   "..." ;
NOTE: Libref DSSOURCE was successfully assigned as follows:
      Engine:        V9
      Physical Name: (system-specific file/path name)
852  +%lookupname(dssource.class );
853  +%put &amp;amp;=varname1;
VARNAME1=police
854  +proc sql;
855  +create table table_info as
856  +  select distinct &amp;amp;varname1. as agreement_nbr length=20
857  +  from dssource.class a
858  +;
NOTE: Table WORK.TABLE_INFO created, with 2 rows and 1 columns.

859  +quit;
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds


860  +libname dssource clear;
NOTE: Libref DSSOURCE has been deassigned.
NOTE: %INCLUDE (level 1) ending.
&lt;/PRE&gt;
&lt;P&gt;And here is our resulting dataset&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1726847684272.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/100567iCC6E3541C715A7D6/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1726847684272.png" alt="Tom_0-1726847684272.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 20 Sep 2024 15:54:51 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2024-09-20T15:54:51Z</dc:date>
    <item>
      <title>ERROR: Column agreement_nbr could not be found in the table/view identified with the correlation nam</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/ERROR-Column-agreement-nbr-could-not-be-found-in-the-table-view/m-p/944752#M45257</link>
      <description>&lt;P&gt;How to solve 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;)',
   /******************** 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 ',strip(cie),' eq be %then %let varname2=datechea;)',
   '%nrstr(%else %if ',strip(cie),' eq gc %then %let varname2=datechea;)',
   '%nrstr(%else %if ',strip(cie),' eq gp %then %let varname2=datechea;)',
   '%nrstr(%else %let varname2=policy_expiry_dt;)',
    '%nrstr(%if ',strip(cie),' eq be %then %let varname3=province;)',
   '%nrstr(%else %if ',strip(cie),' eq gc %then %let varname3=province;)',
   '%nrstr(%else %if ',strip(cie),' eq gp %then %let varname3=province;)',
   '%nrstr(%else %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 ",
   "Select distinct a.&amp;amp;varname1. as agreement_nbr length=20 ",
   "from dssource.",strip(fname),"   a ;",
   "quit;",
   '%nrstr(%mend lookuptbl;)',
   '%nrstr(%lookuptbl;)',	
   "Libname dssource clear;"
)));
run;&lt;/CODE&gt;&lt;/PRE&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 refers to the same physical library as SRC1.&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 be eq be %then %let varname2=datechea;%else %if &lt;BR /&gt;be eq gc %then %let varname2=datechea;%else %if be eq gp %then %let varname2=datechea;%else %let varname2=policy_expiry_dt;%if be &lt;BR /&gt;eq be&lt;BR /&gt;3 + %then %let varname3=province;%else %if be eq gc %then %let varname3=province;%else %if be eq gp %then %let &lt;BR /&gt;varname3=province;%else %let varname3=province_cd;%put &amp;amp;=varname1 &amp;amp;=varname2 &amp;amp;=varname3;proc sql;create table table_info as Select &lt;BR /&gt;distinct&lt;BR /&gt;4 + a.agreement_nbr as agreement_nbr length=20 from dssource.polices a ;quit;%mend lookuptbl;%lookuptbl;Libname dssource &lt;BR /&gt;clear;&lt;BR /&gt;VARNAME1=police VARNAME2=datechea VARNAME3=province&lt;BR /&gt;ERROR: Column agreement_nbr could not be found in the table/view identified with the correlation name A.&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;</description>
      <pubDate>Fri, 20 Sep 2024 14:34:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/ERROR-Column-agreement-nbr-could-not-be-found-in-the-table-view/m-p/944752#M45257</guid>
      <dc:creator>alepage</dc:creator>
      <dc:date>2024-09-20T14:34:05Z</dc:date>
    </item>
    <item>
      <title>Re: ERROR: Column agreement_nbr could not be found in the table/view identified with the correlation</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/ERROR-Column-agreement-nbr-could-not-be-found-in-the-table-view/m-p/944758#M45258</link>
      <description>&lt;P&gt;Why do you think you need to use Call Execut to create multiple versions of the same macro? &lt;/P&gt;
&lt;P&gt;If you have multiple observations in that data set you are creating different versions of the macro and I suspect you are running into a timing a issue of which version is the one that is used.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also you may want to turn on OPTIONS MPRINT to see if the macro code is generating the statements you thing it should.&amp;nbsp; Macro variables that exist int the Lookuptbl macro may be getting changed by your Varexist macro if the same variable name is used in the code of Varexist. If that is happening then you need to explicitly set those variables as %local in Varexist.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Pretty clear:&amp;nbsp; dssource.polices data set does not have a variable named agreement_nbr &lt;STRONG&gt;at the time that particular proc sql executed.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If your %varexist macro is supposed to check for the existence of a variable in data set the bit that Varname1=police means that this "%else %if" never executed&lt;/P&gt;
&lt;PRE&gt;%else %if %varexist (dssource.polices,agreement_nbr) %then %let varname1=agreement_nbr;
&lt;/PRE&gt;
&lt;P&gt;And that somewhere your SQL is using the wrong value of Varname1 which the %put shows as Police but the sql used agreement_nbr.&lt;/P&gt;</description>
      <pubDate>Fri, 20 Sep 2024 15:12:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/ERROR-Column-agreement-nbr-could-not-be-found-in-the-table-view/m-p/944758#M45258</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-09-20T15:12:19Z</dc:date>
    </item>
    <item>
      <title>Re: ERROR: Column agreement_nbr could not be found in the table/view identified with the correlation</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/ERROR-Column-agreement-nbr-could-not-be-found-in-the-table-view/m-p/944760#M45259</link>
      <description>&lt;P&gt;From now on please include logs in the text box (use the &amp;lt;/&amp;gt; icon), like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;VARNAME1=police VARNAME2=datechea VARNAME3=province
&lt;FONT color="#FF0000"&gt;ERROR: Column agreement_nbr could not be found in the table/view identified with the correlation name A.&lt;/FONT&gt;
NOTE: PROC SQL set option NOEXEC and will continue to check the syntax of statements.&lt;/PRE&gt;
&lt;P&gt;There is no such variable agreement_nbr in the data set. You either have the wrong name or the wrong data set (or both). Since we don't have your data sets, you need to investigate this.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;When debugging code with macro variables, it would be helpful to first turn on the macro debugging options by running this line of code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options mprint symbolgen;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and then re-run your code and show us the log for the PROC SQL (if you can't figure it out).&lt;/P&gt;</description>
      <pubDate>Fri, 20 Sep 2024 15:37:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/ERROR-Column-agreement-nbr-could-not-be-found-in-the-table-view/m-p/944760#M45259</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-09-20T15:37:23Z</dc:date>
    </item>
    <item>
      <title>Re: ERROR: Column agreement_nbr could not be found in the table/view identified with the correlation</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/ERROR-Column-agreement-nbr-could-not-be-found-in-the-table-view/m-p/944775#M45260</link>
      <description>&lt;P&gt;First thing is there is NO need to redefine the macro over and over.&amp;nbsp; Just make it an test it to make sure it works.&amp;nbsp; Then your code generation step can use it with confidence.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is a simplified example (I have not idea what you intend to do with that CIE variable and how it is supposed to impact the choice of variable name to use).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro lookupname(dsname);
%if not %symexist(varname1) %then %global varname1;
%let varname1="No variable found";
%if %varexist(&amp;amp;dsname,police) %then %let varname1=police;
%else %if %varexist(&amp;amp;dsname,agreement_nbr) %then %let varname1=agreement_nbr;
%mend;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Let's try it on SASHELP.CLASS and see if it works.&lt;/P&gt;
&lt;PRE&gt;8    %lookupname(sashelp.class);
9    %put &amp;amp;=varname1;
VARNAME1="No variable found"
&lt;/PRE&gt;
&lt;P&gt;Now let's try it on a dataset that does have POLICE variable.&lt;/P&gt;
&lt;PRE&gt;11   data class;
12     set sashelp.class;
13     police=sex;
14   run;

NOTE: The data set WORK.CLASS has 19 observations and 6 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds


15
16   %lookupname(class);
17   %put &amp;amp;=varname1;
VARNAME1=police
&lt;/PRE&gt;
&lt;P&gt;I find it is &lt;STRONG&gt;MUCH easier to just use the PUT statement to generate the code to a FILE&lt;/STRONG&gt; instead of using CALL EXECUTE().&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename code temp;
data _null_;
  file code;
  set .... ;
  put ..... ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That will let you take advantage of the power of the PUT statement (and SAS formats).&amp;nbsp; You won't have to worry about the macro processor interpreting the text before it can get executed (so no more %NRSTR() functions needed).&amp;nbsp; But it also means you have something you can LOOK and make sure the logic for generating the code is working.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  infile code;
  input;
  put _infile_;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So let's try your example program.&amp;nbsp; First let's make a driver dataset that will point to the CLASS dataset we made above that does have a POLICE variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data inforcelisting5;
  length fname $32 cie $8 engine $10 path $200;
  fname='class';
  path=pathname('work');
  output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So let's just make a temporary code file.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename code temp;

data _null_;
  file code ;
  set inforcelisting5;
  put 'Libname dssource ' engine path :$quote. ';'
    / '%lookupname(dssource.' fname ');'
    / '%put &amp;amp;=varname1;'
    / 'proc sql;'
    / 'create table table_info as'
    / '  select distinct &amp;amp;varname1. as agreement_nbr length=20'
    / '  from dssource.' fname 'a'
    / ';'
    / 'quit;'
    / 'libname dssource clear;'
  ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Which will generate this code.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Libname dssource   "..." ;
%lookupname(dssource.class );
%put &amp;amp;=varname1;
proc sql;
create table table_info as
  select distinct &amp;amp;varname1. as agreement_nbr length=20
  from dssource.class a
;
quit;
libname dssource clear;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So let's run it.&lt;/P&gt;
&lt;PRE&gt;850   %include code / source2;
NOTE: %INCLUDE (level 1) file CODE is (system-specific pathname).
851  +Libname dssource   "..." ;
NOTE: Libref DSSOURCE was successfully assigned as follows:
      Engine:        V9
      Physical Name: (system-specific file/path name)
852  +%lookupname(dssource.class );
853  +%put &amp;amp;=varname1;
VARNAME1=police
854  +proc sql;
855  +create table table_info as
856  +  select distinct &amp;amp;varname1. as agreement_nbr length=20
857  +  from dssource.class a
858  +;
NOTE: Table WORK.TABLE_INFO created, with 2 rows and 1 columns.

859  +quit;
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds


860  +libname dssource clear;
NOTE: Libref DSSOURCE has been deassigned.
NOTE: %INCLUDE (level 1) ending.
&lt;/PRE&gt;
&lt;P&gt;And here is our resulting dataset&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1726847684272.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/100567iCC6E3541C715A7D6/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1726847684272.png" alt="Tom_0-1726847684272.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Sep 2024 15:54:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/ERROR-Column-agreement-nbr-could-not-be-found-in-the-table-view/m-p/944775#M45260</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-09-20T15:54:51Z</dc:date>
    </item>
  </channel>
</rss>

