Skip to content
9to5sas
  • Index
  • Glossary
Facebook Twitter Instagram Email RSS

  • Start Here
  • Base SAS
  • Advanced SASExpand
    • SAS Macros
    • PROC SQL
  • SAS/STATSExpand
    • SAS Analytics
    • Statistics
  • SAS Programs
9to5sas

9to5sas » SAS PROGRAMS » Macro To check if a variable exists in SAS dataset

Macro To check if a variable exists in SAS dataset

BySubhro Posted onJuly 22, 2020October 11, 2022 Last Updated:October 11, 2022
0 Comments

Have you ever needed to know if a given variable exists in the SAS data set? Below is a macro that will…

Have you ever needed to know if a given variable exists in the SAS data set? Below is a macro that will check for the existence of a variable in a dataset and will return a value of 0 if the variable does not exist and the column number if the variable exists.

The macro has two required parameters and one optional parameter.

  • DATA parameter accepts 2-level SAS dataset name Example. SASHELP.CARS
  • VAR is the variable name. Example Model
  • INFO is for variable attributes. Example FMT

The %VAREXISTS macro uses Variable functions in a data step to check if a variable exists in SAS and returns the variable info. It will return the below attributes of the variable when appropriate parameters are passed in the INFO parameter. If no value is passed, it returns the column position of the variable by default.

  • Len – To return the length of the variable.
  • fmt – to return the Format of the variable
  • infmt – to return the informal of the variable.
  • label – to return the Label of the variable
  • type – To return the type of variable. (Character of Numeric).

Macro to check if a variable exists in a SAS dataset.

If a variable exists

%put EXISTS: %varexist(sashelp.class,age)

– Returns 3

If a variable does not exist

%put EXISTS: %varexist(work.test,doesnotexist)

– Returns 0

If a Dataset does not exist

%put EXISTS: %varexist(doesnotexist,doesnotexist)

– Returns 0

Variable attribute usage:

%put LENGTH: %varexist(sashelp.class,age,len)

%put FORMAT: %varexist(sashelp.class,age,fmt)

%put INFORMAT: %varexist(sashelp.class,age,infmt)

%put LABEL: %varexist(sashelp.class,age,label)

%put TYPE: %varexist(sashelp.class,age,type)

The code:

options symbolgen mprint mlogic;
%macro varexist(data,var,info);
%local macro parmerr dsis rc varnum;
%let macro = &sysmacroname;
%let dsid = %sysfunc(open(&data));

%if (&dsid) %then %do;
   %let varnum = %sysfunc(varnum(&dsid,&var));

   %if (&varnum) %then %do;
      %if (%length(&info)) %then %do;
         %if (&info eq NUM) %then %do;
&varnum
         %end;
         %else %do;
%sysfunc(var&info(&dsid,&varnum))
         %end;
      %end;
      %else %do;
&varnum
      %end;
   %end;

   %else 0;

   %let rc = %sysfunc(close(&dsid));
%end;

%else 0;
%mend;

/******* END OF FILE *******/
%put EXISTS:   %varexist(sashelp.class,age); 
%put VARNUM:   %varexist(sashelp.class,age,num);
%put LENGTH:   %varexist(sashelp.class,age,len);
%put FORMAT:   %varexist(sashelp.air,date,fmt);
%put INFORMAT: %varexist(sashelp.class,age,infmt);
%put LABEL:    %varexist(sashelp.class,age,label);
%put TYPE:     %varexist(sashelp.class,age,type);

SAS Log:

%put EXISTS: %varexist(sashelp.class,age);
EXISTS: 3
%put VARNUM: %varexist(sashelp.class,age,num);
VARNUM: 0
%put LENGTH: %varexist(sashelp.class,age,len);
LENGTH: 8
%put FORMAT: %varexist(sashelp.air,date,fmt);
FORMAT: MONYY.
%put INFORMAT: %varexist(sashelp.class,age,infmt);
INFORMAT:
%put LABEL: %varexist(sashelp.class,age,label);
LABEL:
%put TYPE: %varexist(sashelp.class,age,type);
TYPE: N

Download this program

Every week we'll send you SAS tips and in-depth tutorials

JOIN OUR COMMUNITY OF SAS Programmers!

Check your inbox or spam folder to confirm your subscription.

Post Tags: #SAS macro Examples
Subhro

Subhro Kar is an Analyst with over five years of experience. As a programmer specializing in SAS (Statistical Analysis System), Subhro also offers tutorials and guides on how to approach the coding language. His website, 9to5sas, offers students and new programmers useful easy-to-grasp resources to help them understand the fundamentals of SAS. Through this website, he shares his passion for programming while giving back to up-and-coming programmers in the field. Subhro’s mission is to offer quality tips, tricks, and lessons that give SAS beginners the skills they need to succeed.

Facebook Twitter Linkedin

Post navigation

Previous Previous
Generating Multiple Excel Files
NextContinue
Why Standardization of variables is important?

SAS Tips in your inbox

Subscribe to 9to5sas for timely SAS tips and news delivered each month.
Learn about the latest articles, and code samples to keep your SAS skills fresh!

Your subscription is successful!

Recent Posts

  • Concatenate strings in SAS: The CAT Functions Demystified
  • 5 Techniques for Quickly Removing Leading Zeros in SAS
  • Troubleshoot Your Proc SQL Code Like a Pro with These 7 Automatic Macro Variables
  • 7 PROC SQL Options You Should Use to Debug Queries
  • How To Use The SAS Proc SQL Order By Statement?
9to5sas
  • Privacy Policy
  • Disclaimer
  • About
  • Contact
Facebook Twitter Instagram RSS Email Pinterest

Copyright © 2023 9TO5SAS, All rights reserved.

Scroll to top
  • 9to5sas Blueprint
  • About
  • About
  • Acceptable use policy
  • calculator
  • Confirm email
  • Contact
  • Contact
  • Cookie Policy
  • DISCLAIMER
  • Getting Started with SAS
  • Glossary
  • Index
  • Post #13801
  • Privacy Policy
  • Privacy policy
  • SAS Programs
  • Styles
  • Subscription confirmed
  • Terms and conditions
  • Thank You