<?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: label If column exists in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/label-If-column-exists/m-p/671501#M201664</link>
    <description>&lt;P&gt;Use PROC CONTENTS to determine what columns you have. Create a macro variable with value 1 if column exists, 0 otherwise, and then use the macro variable to conditionally label the variable.&lt;/P&gt;</description>
    <pubDate>Wed, 22 Jul 2020 17:55:53 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2020-07-22T17:55:53Z</dc:date>
    <item>
      <title>label If column exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/label-If-column-exists/m-p/671499#M201663</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;Is it possible to define label for columns with condition&amp;nbsp; "If column exists"?&lt;/P&gt;
&lt;P&gt;It means that If the column&amp;nbsp; doesn't exist then I will net get an error and the label statement will not be performed.&lt;/P&gt;
&lt;P&gt;For example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data ttt;
Input ID X1 X2 X3;
cards;
1 10 20 30
2 15 25 35
3 18 56 37
;
Run;

Data wanted;
set ttt;
label ID ='Customer ID'
      X1=Height'
     X2='Weight'
     X3='Sex'
    X4='Address'
   X5='email'
;
Run;


&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Jul 2020 17:50:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/label-If-column-exists/m-p/671499#M201663</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2020-07-22T17:50:57Z</dc:date>
    </item>
    <item>
      <title>Re: label If column exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/label-If-column-exists/m-p/671501#M201664</link>
      <description>&lt;P&gt;Use PROC CONTENTS to determine what columns you have. Create a macro variable with value 1 if column exists, 0 otherwise, and then use the macro variable to conditionally label the variable.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Jul 2020 17:55:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/label-If-column-exists/m-p/671501#M201664</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-07-22T17:55:53Z</dc:date>
    </item>
    <item>
      <title>Re: label If column exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/label-If-column-exists/m-p/671584#M201687</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;Is it possible to define label for columns with condition&amp;nbsp; "If column exists"?&lt;/P&gt;
&lt;P&gt;It means that If the column&amp;nbsp; doesn't exist then I will net get an error and the label statement will not be performed.&lt;/P&gt;
&lt;P&gt;For example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data ttt;
Input ID X1 X2 X3;
cards;
1 10 20 30
2 15 25 35
3 18 56 37
;
Run;

Data wanted;
set ttt;
label ID ='Customer ID'
      X1=Height'
     X2='Weight'
     X3='Sex'
    X4='Address'
   X5='email'
;
Run;


&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The only ERROR involved is that you are missing a ' in the label for X1.&lt;/P&gt;
&lt;P&gt;With that fixed there is NO ERROR. Only a note.&lt;/P&gt;
&lt;PRE&gt;140  Data wanted;
141  set ttt;
142  label ID ='Customer ID'
143        X1='Height'
144       X2='Weight'
145       X3='Sex'
146      X4='Address'
147     X5='email'
148  ;
149  Run;

NOTE: Variable X4 is uninitialized.
NOTE: Variable X5 is uninitialized.
NOTE: There were 3 observations read from the data set WORK.TTT.
NOTE: The data set WORK.WANTED has 3 observations and 4 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 Jul 2020 20:04:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/label-If-column-exists/m-p/671584#M201687</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-07-22T20:04:43Z</dc:date>
    </item>
    <item>
      <title>Re: label If column exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/label-If-column-exists/m-p/671655#M201719</link>
      <description>&lt;P&gt;You cannot use SAS code to conditionally execute the LABEL statement, since there is nothing to execute in a LABEL statement since all it does is setup the metadata about the data you are using.&amp;nbsp; You need to conditionally generate the code.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The SAS macro processor is designed for that.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The hard part is checking if the variable is in the input dataset.&amp;nbsp; You need to do that before your data step runs as macro processor statements are evaluated before the code is passed to SAS to be run.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are a number of utility macros out there that make it a little easier.&amp;nbsp; Such as&amp;nbsp;&lt;A href="https://github.com/sasutils/macros/blob/master/varexist.sas" target="_self"&gt;varexist&lt;/A&gt;&amp;nbsp;.&lt;/P&gt;
&lt;P&gt;For example this data step will always have LABEL statements for ID, X1 to X3, but only add label statements for X4 and/or X5 if they exist in the source dataset TTT.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set ttt;
  label ID ='Customer ID' ;
  label X1='Height' ;
  label X2='Weight';
  label X3='Sex';
%if %varexist(ttt,x4) %then %do;
  label X4='Address';
%end;
%if %varexist(ttt,x5) %then %do;
  label X5='email' ;
%end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 23 Jul 2020 02:54:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/label-If-column-exists/m-p/671655#M201719</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-07-23T02:54:57Z</dc:date>
    </item>
    <item>
      <title>Re: label If column exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/label-If-column-exists/m-p/671657#M201720</link>
      <description>&lt;P&gt;Do not recreate a table just to change the metadata.&lt;/P&gt;
&lt;P&gt;To reuse&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;'s macro, and provided you have the latest version of SAS:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc datasets noprint ; 
%if %varexist(TTT,X3) %then %do; modify TTT;label X3='abc';run; %end;
%if %varexist(TTT,X4) %then %do; modify TTT;label X4='def';run; %end;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you have on older version:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc datasets noprint ; 
%sysfunc(ifc( %varexist(TTT,X3), %str(modify TTT;label X3='abc';run;), ));
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Jul 2020 03:29:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/label-If-column-exists/m-p/671657#M201720</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-07-23T03:29:02Z</dc:date>
    </item>
  </channel>
</rss>

