<?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 a field exist and if not to create a field with missing value in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/check-if-a-field-exist-and-if-not-to-create-a-field-with-missing/m-p/613008#M178980</link>
    <description>&lt;P&gt;Or use a function-style macro to create a boolean value:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro check_column(lib=,ds=,var=);
%let did=%sysfunc(open(&amp;amp;lib..&amp;amp;ds));
%let pos=%sysfunc(varnum(&amp;amp;did,&amp;amp;var));
&amp;amp;pos
%let did=%sysfunc(close(&amp;amp;did));
%mend;

data tbl2;
set tbl1;
if ^%check_column(lib=work,ds=tbl1,var=x3) then x3 = .;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&lt;FONT size="1 2 3 4 5 6 7"&gt;Edit: added close in macro&lt;/FONT&gt;&lt;/EM&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 19 Dec 2019 12:44:26 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2019-12-19T12:44:26Z</dc:date>
    <item>
      <title>check if a field exist and if not to create a field with missing value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-if-a-field-exist-and-if-not-to-create-a-field-with-missing/m-p/612999#M178976</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;I have multiple&amp;nbsp; &amp;nbsp;data sets that I need to work with.&lt;/P&gt;
&lt;P&gt;The problem is that in some&amp;nbsp; data sets&amp;nbsp; some fields are not existing.&lt;/P&gt;
&lt;P&gt;I will&amp;nbsp; illustrate the issue with a simple problem.&lt;/P&gt;
&lt;P&gt;What is the way to check if a field exist and if not to create a field with missing value&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;
data tbl1b;
input ID x1 x2 ;
cards;
1 10 15
2 20 25
3 30 35
4 40 45
5 50 55
;
run;


Data tbl2;
set tbl1;
IF x3 not exist then x3=.;
Run;

Data tbl2b;
set tbl1b;
IF x3 not exist then x3=.;
Run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 19 Dec 2019 12:14:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-if-a-field-exist-and-if-not-to-create-a-field-with-missing/m-p/612999#M178976</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2019-12-19T12:14:46Z</dc:date>
    </item>
    <item>
      <title>Re: check if a field exist and if not to create a field with missing value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-if-a-field-exist-and-if-not-to-create-a-field-with-missing/m-p/613004#M178977</link>
      <description>&lt;P&gt;Idea: create an empty dataset with the expected structure and use if 0 then set in a data step:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ExpectedStructure;
   length Id x1-x3 8;
   call missing(of _all_); /* prevents not-initialized notes/warnings/errors */
   stop;
run;


data tbl2;
   if 0 then set ExpectedStructure;
   set tbl1;
run;

data tbl2b;
   if 0 then set ExpectedStructure;
   set tbl1b;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 19 Dec 2019 12:29:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-if-a-field-exist-and-if-not-to-create-a-field-with-missing/m-p/613004#M178977</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-12-19T12:29:02Z</dc:date>
    </item>
    <item>
      <title>Re: check if a field exist and if not to create a field with missing value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-if-a-field-exist-and-if-not-to-create-a-field-with-missing/m-p/613005#M178978</link>
      <description>&lt;P&gt;Very simple approach: query dictionary.columns:&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;

data tbl2;
set tbl1;
if not &amp;amp;varexist then x3 = .;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 19 Dec 2019 12:29:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-if-a-field-exist-and-if-not-to-create-a-field-with-missing/m-p/613005#M178978</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-12-19T12:29:02Z</dc:date>
    </item>
    <item>
      <title>Re: check if a field exist and if not to create a field with missing value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-if-a-field-exist-and-if-not-to-create-a-field-with-missing/m-p/613006#M178979</link>
      <description>&lt;P&gt;You could create a null sample dataset , and append it into original tables by PROC APPEND or SQL.&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;
data tbl1b;
input ID x1 x2 ;
cards;
1 10 15
2 20 25
3 30 35
4 40 45
5 50 55
;
run;


proc sql nowarn;
create table sample (id num,x1 num,x2 num,x3 num) ;

create table new1 as
select * from sample 
outer union corr
select * from tbl1;

create table new2 as
select * from sample 
outer union corr
select * from tbl1b;

quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Dec 2019 12:35:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-if-a-field-exist-and-if-not-to-create-a-field-with-missing/m-p/613006#M178979</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-12-19T12:35:51Z</dc:date>
    </item>
    <item>
      <title>Re: check if a field exist and if not to create a field with missing value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-if-a-field-exist-and-if-not-to-create-a-field-with-missing/m-p/613008#M178980</link>
      <description>&lt;P&gt;Or use a function-style macro to create a boolean value:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro check_column(lib=,ds=,var=);
%let did=%sysfunc(open(&amp;amp;lib..&amp;amp;ds));
%let pos=%sysfunc(varnum(&amp;amp;did,&amp;amp;var));
&amp;amp;pos
%let did=%sysfunc(close(&amp;amp;did));
%mend;

data tbl2;
set tbl1;
if ^%check_column(lib=work,ds=tbl1,var=x3) then x3 = .;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&lt;FONT size="1 2 3 4 5 6 7"&gt;Edit: added close in macro&lt;/FONT&gt;&lt;/EM&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Dec 2019 12:44:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-if-a-field-exist-and-if-not-to-create-a-field-with-missing/m-p/613008#M178980</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-12-19T12:44:26Z</dc:date>
    </item>
    <item>
      <title>Re: check if a field exist and if not to create a field with missing value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-if-a-field-exist-and-if-not-to-create-a-field-with-missing/m-p/613031#M178989</link>
      <description>&lt;P&gt;Perhaps a simple solution would do?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   x3 = x3;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If X3 already exists, the program leaves it as is.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if X3 doesn't already exist, the program creates it as numeric, with a missing value.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Dec 2019 13:44:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-if-a-field-exist-and-if-not-to-create-a-field-with-missing/m-p/613031#M178989</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-12-19T13:44:51Z</dc:date>
    </item>
    <item>
      <title>Re: check if a field exist and if not to create a field with missing value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-if-a-field-exist-and-if-not-to-create-a-field-with-missing/m-p/613037#M178991</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As long as you use the variable somewhere in the data step, it will exist in the&lt;/P&gt;
&lt;P&gt;resulting dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
set sashelp.class;
if missing(x3) then put _N_ "missing value found for X3";
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If x3 does not exist, it will be given a default numeric best. format (a note about x3 being uninitialized will also be issued).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to specific column attributes you can set them with length, format, informat, attrib instructions which will also&lt;/P&gt;
&lt;P&gt;have the effect of creating the column in case it does not exist.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Dec 2019 13:58:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-if-a-field-exist-and-if-not-to-create-a-field-with-missing/m-p/613037#M178991</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2019-12-19T13:58:59Z</dc:date>
    </item>
    <item>
      <title>Re: check if a field exist and if not to create a field with missing value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-if-a-field-exist-and-if-not-to-create-a-field-with-missing/m-p/613064#M179002</link>
      <description>&lt;P&gt;It is trivial for numeric variables since you can add a LENGTH statement.&amp;nbsp; If the variable exists it doesn't change it, other than perhaps changing how many of the 8 byte floating point value is actually stored into the output dataset(s).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data tbl2;
  set tbl1;
  length x3 8 ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If X3 is in TBL1 then its value is unchanged. If the variable X3 was not in TBL1 then it is created and you will get a nice little note in the log from SAS saying that it has not been initialized.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Dec 2019 14:55:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-if-a-field-exist-and-if-not-to-create-a-field-with-missing/m-p/613064#M179002</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-12-19T14:55:14Z</dc:date>
    </item>
    <item>
      <title>Re: check if a field exist and if not to create a field with missing value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-if-a-field-exist-and-if-not-to-create-a-field-with-missing/m-p/666926#M199637</link>
      <description>Thank you, worked great!</description>
      <pubDate>Sat, 04 Jul 2020 07:19:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-if-a-field-exist-and-if-not-to-create-a-field-with-missing/m-p/666926#M199637</guid>
      <dc:creator>Mar_Lds</dc:creator>
      <dc:date>2020-07-04T07:19:26Z</dc:date>
    </item>
  </channel>
</rss>

