<?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: Detect if Column Exists in a Table in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Detect-if-Column-Exists-in-a-Table/m-p/818352#M323025</link>
    <description>&lt;P&gt;Code works fine once you fix all the syntax errors.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data tbl1;
input ID x1 x2 ;
cards;
1 10 15
2 20 25
3 30 35
4 40 45
5 50 55
;

run;

proc sql noprint;
select count(*) into :varexist
from dictionary.columns
where libname="WORK" and memname="TBL1" and upcase(name)="X3";

quit;

%put &amp;amp;varexist;


data tbl2;

set tbl1;

if not &amp;amp;varexist then x3=.;

run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 15 Jun 2022 15:37:39 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2022-06-15T15:37:39Z</dc:date>
    <item>
      <title>Detect if Column Exists in a Table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Detect-if-Column-Exists-in-a-Table/m-p/818334#M323012</link>
      <description>&lt;P&gt;There are tables for different dates where the column name changed for some reason.&amp;nbsp; I'm trying to: 1). detect if the column of a specific name exists in the table.&amp;nbsp; 2). If it exists in the table, then in PROC SQL SELECT it will use that column name.&amp;nbsp; If the specified column name does not exist in the table (i.e., it has a slightly different naming convention), then I want PROC SQL SELECT to use an alternative column name that I specify in advance (it appears that only two different naming conventions were used for the subject column).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code I am testing to identify a specified missing column (item #1) was taken from a different SAS Community topic, but it doesn't appear to work properly:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data tbl1;

input ID x1,x2 ;

cards;

1 10 15

2 20 25

3 30 35

4 40 45

5 50 55

;

run;

proc sql noprint;

select count(*) into :varexist

from dictionary.columns

where libname="WORK" and memname="TBL1" and upcase(name)="X3";

quit;

data tbl2;

set tbl1;

if not &amp;amp;varexist then x3=.;

run;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;If I use&amp;nbsp;upcase(name)="X3" or&amp;nbsp;upcase(name)="X2", a column X3 is created with "." values.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thoughts on how item #1 and #2 can be accomplished?&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jun 2022 15:36:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Detect-if-Column-Exists-in-a-Table/m-p/818334#M323012</guid>
      <dc:creator>bobsas1</dc:creator>
      <dc:date>2022-06-15T15:36:25Z</dc:date>
    </item>
    <item>
      <title>Re: Detect if Column Exists in a Table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Detect-if-Column-Exists-in-a-Table/m-p/818336#M323014</link>
      <description>&lt;P&gt;I will leave it up to you to fix the typos in your code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Before PROC SQL, you need to add the following:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let varlist=0;&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;From now on, please paste code into the box that appears when you click on the "little running man" icon.&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jun 2022 15:12:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Detect-if-Column-Exists-in-a-Table/m-p/818336#M323014</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-06-15T15:12:19Z</dc:date>
    </item>
    <item>
      <title>Re: Detect if Column Exists in a Table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Detect-if-Column-Exists-in-a-Table/m-p/818339#M323017</link>
      <description>&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Programming/SAS-query/m-p/350433#M81457" target="_blank"&gt;https://communities.sas.com/t5/SAS-Programming/SAS-query/m-p/350433#M81457&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;here is a topic that shows how to check if a variable exists in a dataset. you should be able to modify that to do what you want when the variable does not exist.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jun 2022 15:15:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Detect-if-Column-Exists-in-a-Table/m-p/818339#M323017</guid>
      <dc:creator>tarheel13</dc:creator>
      <dc:date>2022-06-15T15:15:56Z</dc:date>
    </item>
    <item>
      <title>Re: Detect if Column Exists in a Table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Detect-if-Column-Exists-in-a-Table/m-p/818346#M323022</link>
      <description>&lt;P&gt;When I clean up your code and run the Proc SQL adding this:&lt;/P&gt;
&lt;PRE&gt;20   %put Varexist is: &amp;amp;varexist;
Varexist is:        0
&lt;/PRE&gt;
&lt;P&gt;The result for Varexist is zero as shown.&lt;/P&gt;
&lt;P&gt;So in the following data step you have submitted:&lt;/P&gt;
&lt;PRE&gt;data tbl2;
   set tbl1;
   if not 0 then x3=.;
run;&lt;/PRE&gt;
&lt;P&gt;So I am not sure what you are actually attempting here. "Not 0" is always true in SAS: 0 is treated as false so "not false"=true.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Personally, I would go back to when the data is read into SAS to see why the variable names are different and address the issue at that point. One suspects Proc Import is involved.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have run into this where the data source changes column headings in source files but since I read them with data steps then as long as the values are in the correct column and of the appropriate values then no problems. Plus the data steps generally throw data errors when the column order gets changed (they do that to).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or us Proc Datasets to change the name of the variable in those sets since you say there seem to be two names.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jun 2022 15:24:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Detect-if-Column-Exists-in-a-Table/m-p/818346#M323022</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-06-15T15:24:55Z</dc:date>
    </item>
    <item>
      <title>Re: Detect if Column Exists in a Table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Detect-if-Column-Exists-in-a-Table/m-p/818352#M323025</link>
      <description>&lt;P&gt;Code works fine once you fix all the syntax errors.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data tbl1;
input ID x1 x2 ;
cards;
1 10 15
2 20 25
3 30 35
4 40 45
5 50 55
;

run;

proc sql noprint;
select count(*) into :varexist
from dictionary.columns
where libname="WORK" and memname="TBL1" and upcase(name)="X3";

quit;

%put &amp;amp;varexist;


data tbl2;

set tbl1;

if not &amp;amp;varexist then x3=.;

run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 15 Jun 2022 15:37:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Detect-if-Column-Exists-in-a-Table/m-p/818352#M323025</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-06-15T15:37:39Z</dc:date>
    </item>
  </channel>
</rss>

