<?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: check if column exists &amp;amp; add if not in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/check-if-column-exists-amp-add-if-not/m-p/351319#M273687</link>
    <description>&lt;P&gt;okay, i got the answer.&amp;nbsp; i combined novinosrin &amp;amp; tom &amp;amp; astoundings answers.&amp;nbsp; when i originally was using the link from novinosrin, when i made a change it would not hold the change.&amp;nbsp; i think what it was doing was opening the table early in the code, holding it open through the checks and where the changes were made and that was making a change to the open version of the file but not the permanent file so i moved the close line up and put the result into a variable and things work great now.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks for the help, here is the working code i am using...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro VarExist(ds, var);&lt;BR /&gt;%local rc dsid result resx;&lt;BR /&gt;%let dsid = %sysfunc(open(&amp;amp;ds));&lt;BR /&gt;%let resx = %sysfunc(varnum(&amp;amp;dsid, &amp;amp;var));&lt;BR /&gt;%let rc = %sysfunc(close(&amp;amp;dsid));&lt;BR /&gt;&lt;BR /&gt;%if &amp;amp;resx &amp;gt; 0 %then %do;&lt;BR /&gt;%let result = 1;&lt;BR /&gt;%put NOTE: Var &amp;amp;var exists in &amp;amp;ds;&lt;BR /&gt;%end;&lt;BR /&gt;&lt;BR /&gt;%else %do;&lt;BR /&gt;%let result = 0;&lt;BR /&gt;%put NOTE: Var &amp;amp;var not exists in &amp;amp;ds;&lt;BR /&gt;&lt;BR /&gt;data &amp;amp;dsid;&lt;BR /&gt;&amp;amp;var=_n_;&lt;BR /&gt;set &amp;amp;dsid;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;%end;&lt;BR /&gt;%mend VarExist;&lt;BR /&gt;&lt;BR /&gt;%VarExist(TABLE, FIELD);&lt;/P&gt;</description>
    <pubDate>Wed, 19 Apr 2017 14:28:50 GMT</pubDate>
    <dc:creator>me55</dc:creator>
    <dc:date>2017-04-19T14:28:50Z</dc:date>
    <item>
      <title>check if column exists &amp; add if not</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-if-column-exists-amp-add-if-not/m-p/350918#M273679</link>
      <description>&lt;P&gt;okay, i have kind of an odd situation where the table i am importing into sas might or might not have a needed column of data.&amp;nbsp; if it does not then i need to add that column to the table.&amp;nbsp; i was just going to add the column using sql but i need help figuring out how write that check to see if the column exists first.&amp;nbsp; can anyone help me with that.&amp;nbsp; i think i am getting close just cant nail it down...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;so basically something like...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if table.column does not exist,&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;alter table add column char(20);&lt;/P&gt;&lt;P&gt;(or else check another column...)&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Apr 2017 16:19:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-if-column-exists-amp-add-if-not/m-p/350918#M273679</guid>
      <dc:creator>me55</dc:creator>
      <dc:date>2017-04-18T16:19:14Z</dc:date>
    </item>
    <item>
      <title>Re: check if column exists &amp; add if not</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-if-column-exists-amp-add-if-not/m-p/350920#M273680</link>
      <description>&lt;P&gt;&lt;A href="https://communities.sas.com/t5/Base-SAS-Programming/SAS-query/m-p/350433#M81457" target="_blank"&gt;https://communities.sas.com/t5/Base-SAS-Programming/SAS-query/m-p/350433#M81457&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;check the above link, you should have your solution&lt;/P&gt;</description>
      <pubDate>Tue, 18 Apr 2017 16:22:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-if-column-exists-amp-add-if-not/m-p/350920#M273680</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2017-04-18T16:22:37Z</dc:date>
    </item>
    <item>
      <title>Re: check if column exists &amp; add if not</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-if-column-exists-amp-add-if-not/m-p/350925#M273681</link>
      <description>&lt;P&gt;The link &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;shared has several solutions. The last one, not the accepted solution, may be the easiest to implement and maintain long term.&lt;/P&gt;</description>
      <pubDate>Tue, 18 Apr 2017 16:38:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-if-column-exists-amp-add-if-not/m-p/350925#M273681</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-04-18T16:38:13Z</dc:date>
    </item>
    <item>
      <title>Re: check if column exists &amp; add if not</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-if-column-exists-amp-add-if-not/m-p/350927#M273682</link>
      <description>&lt;P&gt;Given that you are looking to add a character variable, and you know the length should be 20, you would need a slight modification:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;length column_name $ 20;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Apr 2017 16:41:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-if-column-exists-amp-add-if-not/m-p/350927#M273682</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-04-18T16:41:11Z</dc:date>
    </item>
    <item>
      <title>Re: check if column exists &amp; add if not</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-if-column-exists-amp-add-if-not/m-p/350937#M273683</link>
      <description>&lt;P&gt;oh sorry, i just put that in there.&amp;nbsp; the column i am looking to add is an id column that will be autonumbered via _n_...&lt;/P&gt;</description>
      <pubDate>Tue, 18 Apr 2017 17:03:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-if-column-exists-amp-add-if-not/m-p/350937#M273683</guid>
      <dc:creator>me55</dc:creator>
      <dc:date>2017-04-18T17:03:11Z</dc:date>
    </item>
    <item>
      <title>Re: check if column exists &amp; add if not</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-if-column-exists-amp-add-if-not/m-p/350939#M273684</link>
      <description>&lt;P&gt;MUCH easier if you do NOT use PROC SQL.&lt;/P&gt;
&lt;P&gt;So if you requre that the variable MUST_EXIST is on the data and it should be a number then you could just run this step after "importing" the data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
   length must_exist 8 ;
   set have ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Only trouble you will get is if the variable DID exist, but was of the wrong type. &amp;nbsp;Or it is was character and had values longer than what you specified in your code which would cause truncation of the long values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to add a record counter and when the variable does exist (or it exists with missing values) set it to the observations number then you might be able to use the automatic step counter varaible _N_.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
   length recno 8 ;
   set have ;
   recno = coalesce(recno,_n_);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 18 Apr 2017 17:15:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-if-column-exists-amp-add-if-not/m-p/350939#M273684</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-04-18T17:15:09Z</dc:date>
    </item>
    <item>
      <title>Re: check if column exists &amp; add if not</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-if-column-exists-amp-add-if-not/m-p/350948#M273685</link>
      <description>&lt;P&gt;the thing is the field may exist or not.&amp;nbsp; basically i need something that will check if the field exists alread in the table, if it does not, add it and if it does then do nothing.&amp;nbsp; after i import the table, it might be there and it might not.&amp;nbsp; i need it to check that and i need it to run within the context of a larger program and not error out and stop the rest of the program from running AND i need it set to the table because this field is going to be important later on in the process.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;when i tried, within the context of that macro (the link above...)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;set TABLE;&lt;/P&gt;&lt;P&gt;ID=_n_;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;it added the column but not permanently to the table so in later on in the program when this field is important.&amp;nbsp; this field is a marking field for data identification later on.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Apr 2017 17:44:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-if-column-exists-amp-add-if-not/m-p/350948#M273685</guid>
      <dc:creator>me55</dc:creator>
      <dc:date>2017-04-18T17:44:35Z</dc:date>
    </item>
    <item>
      <title>Re: check if column exists &amp; add if not</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-if-column-exists-amp-add-if-not/m-p/350951#M273686</link>
      <description>&lt;P&gt;OK, for that:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;id = _n_;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If HAVE contains ID, its values will overwrite the values copied from _N_.&lt;/P&gt;</description>
      <pubDate>Tue, 18 Apr 2017 17:58:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-if-column-exists-amp-add-if-not/m-p/350951#M273686</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-04-18T17:58:20Z</dc:date>
    </item>
    <item>
      <title>Re: check if column exists &amp; add if not</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-if-column-exists-amp-add-if-not/m-p/351319#M273687</link>
      <description>&lt;P&gt;okay, i got the answer.&amp;nbsp; i combined novinosrin &amp;amp; tom &amp;amp; astoundings answers.&amp;nbsp; when i originally was using the link from novinosrin, when i made a change it would not hold the change.&amp;nbsp; i think what it was doing was opening the table early in the code, holding it open through the checks and where the changes were made and that was making a change to the open version of the file but not the permanent file so i moved the close line up and put the result into a variable and things work great now.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks for the help, here is the working code i am using...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro VarExist(ds, var);&lt;BR /&gt;%local rc dsid result resx;&lt;BR /&gt;%let dsid = %sysfunc(open(&amp;amp;ds));&lt;BR /&gt;%let resx = %sysfunc(varnum(&amp;amp;dsid, &amp;amp;var));&lt;BR /&gt;%let rc = %sysfunc(close(&amp;amp;dsid));&lt;BR /&gt;&lt;BR /&gt;%if &amp;amp;resx &amp;gt; 0 %then %do;&lt;BR /&gt;%let result = 1;&lt;BR /&gt;%put NOTE: Var &amp;amp;var exists in &amp;amp;ds;&lt;BR /&gt;%end;&lt;BR /&gt;&lt;BR /&gt;%else %do;&lt;BR /&gt;%let result = 0;&lt;BR /&gt;%put NOTE: Var &amp;amp;var not exists in &amp;amp;ds;&lt;BR /&gt;&lt;BR /&gt;data &amp;amp;dsid;&lt;BR /&gt;&amp;amp;var=_n_;&lt;BR /&gt;set &amp;amp;dsid;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;%end;&lt;BR /&gt;%mend VarExist;&lt;BR /&gt;&lt;BR /&gt;%VarExist(TABLE, FIELD);&lt;/P&gt;</description>
      <pubDate>Wed, 19 Apr 2017 14:28:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-if-column-exists-amp-add-if-not/m-p/351319#M273687</guid>
      <dc:creator>me55</dc:creator>
      <dc:date>2017-04-19T14:28:50Z</dc:date>
    </item>
  </channel>
</rss>

