BookmarkSubscribeRSS Feed
Ronein
Onyx | Level 15

Hello

Is it possible to define label for columns with condition  "If column exists"?

It means that If the column  doesn't exist then I will net get an error and the label statement will not be performed.

For example:

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;


 

4 REPLIES 4
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
ballardw
Super User

@Ronein wrote:

Hello

Is it possible to define label for columns with condition  "If column exists"?

It means that If the column  doesn't exist then I will net get an error and the label statement will not be performed.

For example:

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;


 


The only ERROR involved is that you are missing a ' in the label for X1.

With that fixed there is NO ERROR. Only a note.

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
Tom
Super User Tom
Super User

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.  You need to conditionally generate the code. 

 

The SAS macro processor is designed for that.

 

The hard part is checking if the variable is in the input dataset.  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. 

There are a number of utility macros out there that make it a little easier.  Such as varexist .

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.

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;
ChrisNZ
Tourmaline | Level 20

Do not recreate a table just to change the metadata.

To reuse @Tom 's macro, and provided you have the latest version of SAS:

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;

If you have on older version:

proc datasets noprint ; 
%sysfunc(ifc( %varexist(TTT,X3), %str(modify TTT;label X3='abc';run;), ));
quit;

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 2225 views
  • 0 likes
  • 5 in conversation