BookmarkSubscribeRSS Feed
SASPhile
Quartz | Level 8

How to drop a variable if it exists?There are hynds of  datasets and not all datasets have that variable.

2 REPLIES 2
BenbowL
Fluorite | Level 6

Hey SASPhile,

 

Two methods spring to mind ... one is to interogate the sashelp.vcolumn dataset to see if the variable exists there then conditionally write the variable into your datastep using a macro.  Second (and I'm not sure how others will feel about this option) is to use

option DKRICOND=NOWARN; before the datastep with option DKRICOND=ERROR; after the datastep .  Setting DKRICOND to nowarn before your datastep will suppress the error message that would normally be generated by trying to drop a variable that does not exist and will let your datastep execute.  As I say, this probably isn't best practice, but dependent on your data may be a simpler solution.

 

Good luck!

novinosrin
Tourmaline | Level 20

@SASPhile You might find this useful, call this macro to your convenience:

 

 

 

%macro VarExist(ds, var);

    %local rc dsid;

    %let dsid = %sysfunc(open(&ds));

 

    %if %sysfunc(varnum(&dsid, &var)) > 0 %then %do;

        %let result = 1;

        %put NOTE: Var &var exists in &ds;

           drop &var;

    %end;

    %else %do;

        %let result = 0;

        %put NOTE: Var &var not exists in &ds;

    %end;

 

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

 

%mend VarExist;

 

 %VarExist(sashelp.class, name);

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 5015 views
  • 4 likes
  • 3 in conversation