<?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: Checking if a field exists in a datastep in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Checking-if-a-field-exists-in-a-datastep/m-p/470948#M120563</link>
    <description>&lt;P&gt;Thanks Reeza, with a few minor modifications to your code I was able to get mine working.&lt;/P&gt;</description>
    <pubDate>Mon, 18 Jun 2018 00:10:45 GMT</pubDate>
    <dc:creator>chris_e</dc:creator>
    <dc:date>2018-06-18T00:10:45Z</dc:date>
    <item>
      <title>Checking if a field exists in a datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Checking-if-a-field-exists-in-a-datastep/m-p/470492#M120416</link>
      <description>&lt;P&gt;I have a file that can have one of two fields site of cov in a table called A1_loc. If the field is Site I want fields 1 and 2 to have "SA" in them and if it is cov I want those fields to have "C" in them. I have tried writing the code in a number of different ways using varnum and varexist, but neither seems to be working. Below is an example of my code with varnum:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;%macro test();

	data test1;
		set A1_loc;
		%if varnum('A1_Loc', 'site') &amp;gt; 0 %then %do;
			field1 = "SA";
			field2 = "SA";
		%end
		%else %if varnum('A1_Loc', 'cov') &amp;gt; 0 %then %do;
			field1 = "C";
			field2 = "C";
		%end;
	run;

%mend;

%test();&lt;/PRE&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Jun 2018 01:03:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Checking-if-a-field-exists-in-a-datastep/m-p/470492#M120416</guid>
      <dc:creator>chris_e</dc:creator>
      <dc:date>2018-06-15T01:03:37Z</dc:date>
    </item>
    <item>
      <title>Re: Checking if a field exists in a datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Checking-if-a-field-exists-in-a-datastep/m-p/470495#M120418</link>
      <description>&lt;P&gt;VARNUM() function is used differently than how you're trying to use it.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;An example is here:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://documentation.sas.com/?docsetId=mcrolref&amp;amp;docsetTarget=n0s4xeo2nzqhdtn177qgad6iz69f.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_blank"&gt;http://documentation.sas.com/?docsetId=mcrolref&amp;amp;docsetTarget=n0s4xeo2nzqhdtn177qgad6iz69f.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You'd have to open the dataset using OPEN() and then use the functions to check.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Some other options are using the SASHELP.VCOLUMN table which has the variable list to create your logic and another is using CALL VNEXT which loops through all variables in the data set.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's an example of how that may work. Note that character comparisons are case sensitive so I used the upcase function here.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*both variables exist;

data class1;
    set sashelp.class;
    site='1';
    A1_loc='2';
    drop name;
run;

*one variable exists;

data class2;
    set sashelp.class;
    site='1';
    drop name;
run;

*neither variable exists;

data class3;
    set sashelp.class;
    drop name;
run;

%macro check_var_exist(dsn=);
    data test;
        set &amp;amp;dsn end=eof;
        length name $32 type $3;
        name=' ';
        length=666;
        var1=0;
        var2=0;

        do i=1 to 99 until(name=' ');
            call vnext(name, type, length);

            if upcase(name)='A1_LOC' then
                var1=1;

            if upcase(name)='SITE' then
                var2=1;
        end;

        if eof then
            do;

                if var1 and var2 then
                    put "&amp;amp;dsn: Both variables found";
                else
                    put "&amp;amp;dsn: Not found";
            end;
    run;

%mend;

%check_var_exist(dsn=class1);
%check_var_exist(dsn=class2);
%check_var_exist(dsn=class3);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/55073"&gt;@chris_e&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have a file that can have one of two fields site of cov in a table called A1_loc. If the field is Site I want fields 1 and 2 to have "SA" in them and if it is cov I want those fields to have "C" in them. I have tried writing the code in a number of different ways using varnum and varexist, but neither seems to be working. Below is an example of my code with varnum:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%macro test();

	data test1;
		set A1_loc;
		%if varnum('A1_Loc', 'site') &amp;gt; 0 %then %do;
			field1 = "SA";
			field2 = "SA";
		%end
		%else %if varnum('A1_Loc', 'cov') &amp;gt; 0 %then %do;
			field1 = "C";
			field2 = "C";
		%end;
	run;

%mend;

%test();&lt;/PRE&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Jun 2018 01:16:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Checking-if-a-field-exists-in-a-datastep/m-p/470495#M120418</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-06-15T01:16:01Z</dc:date>
    </item>
    <item>
      <title>Re: Checking if a field exists in a datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Checking-if-a-field-exists-in-a-datastep/m-p/470506#M120424</link>
      <description>&lt;P&gt;If I'm reading the problem correctly:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;proc contents data=A1_loc noprint out=_contents_ (keep=name);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;run;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;%let field=COV;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;data _null_;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp;set _contents_;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp;where upcase(name)='SITE';&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp;call symput(field, 'SITE');&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;run;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;At this point, the macro variable &amp;amp;FIELD contains either COV or SITE, depending on whether SITE was found in the data set.&amp;nbsp; To continue:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;data want;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;set A1_loc;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;if "&amp;amp;field"='SITE' then do;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp;field1 = 'SA';&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp;field2 = 'SA';&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;end;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;else do;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp;field1 = 'C';&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp;field2 = 'C';&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;end;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;run;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You certainly can use dictionary.columns instead of PROC CONTENTS if that is your preference.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Jun 2018 02:52:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Checking-if-a-field-exists-in-a-datastep/m-p/470506#M120424</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-06-15T02:52:47Z</dc:date>
    </item>
    <item>
      <title>Re: Checking if a field exists in a datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Checking-if-a-field-exists-in-a-datastep/m-p/470509#M120425</link>
      <description>&lt;P&gt;This might do:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
retain cov ._; /* Some impossible value */
set a1_loc;
if cov = ._ then field1 = "SA";
else field1 = "C";
field2 = field1;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 15 Jun 2018 03:31:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Checking-if-a-field-exists-in-a-datastep/m-p/470509#M120425</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-06-15T03:31:55Z</dc:date>
    </item>
    <item>
      <title>Re: Checking if a field exists in a datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Checking-if-a-field-exists-in-a-datastep/m-p/470514#M120426</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data TEST1;
  if _N_=1 then do;
    DSID=open('A1_LOC');
    DO_SITE+varnum(DSID,'SITE');
    RC=close(DSID);
  end;
  set A1_LOC;
  if DO_SITE then do;
    FIELD1 = "SA";
    FIELD2 = "SA";
  end;
  else do;
    FIELD1 = "C";
    FIELD2 = "C";
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since you obviously have a limited understanding of the macro language (you are using it inappropriately here by wrongly running&amp;nbsp;macro statements in that data step ), try to use it as little as possible.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is a rule even seasoned macro language&amp;nbsp;programmers follow.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The macro language&amp;nbsp;is not needed here. Do not use it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Jun 2018 04:26:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Checking-if-a-field-exists-in-a-datastep/m-p/470514#M120426</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-06-15T04:26:08Z</dc:date>
    </item>
    <item>
      <title>Re: Checking if a field exists in a datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Checking-if-a-field-exists-in-a-datastep/m-p/470523#M120429</link>
      <description>&lt;P&gt;Keep it simple:&lt;/P&gt;
&lt;PRE&gt;data test1;
  length site cov $50;
  set a1_loc;
  if site ne '' then do;
    field1='SA';
    field2='SA';
  end;
  else do;
    field1='C';
    field2='C';
  end;
run
&lt;/PRE&gt;
&lt;P&gt;The length will create the variables, and then any data from the individual contibutors will feed into it, being missing otherwise.&lt;/P&gt;
&lt;P&gt;Why do you need two variables exactly the same (field1/2)?&lt;/P&gt;</description>
      <pubDate>Fri, 15 Jun 2018 07:44:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Checking-if-a-field-exists-in-a-datastep/m-p/470523#M120429</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-06-15T07:44:23Z</dc:date>
    </item>
    <item>
      <title>Re: Checking if a field exists in a datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Checking-if-a-field-exists-in-a-datastep/m-p/470948#M120563</link>
      <description>&lt;P&gt;Thanks Reeza, with a few minor modifications to your code I was able to get mine working.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Jun 2018 00:10:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Checking-if-a-field-exists-in-a-datastep/m-p/470948#M120563</guid>
      <dc:creator>chris_e</dc:creator>
      <dc:date>2018-06-18T00:10:45Z</dc:date>
    </item>
  </channel>
</rss>

