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

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 5245 views
  • 4 likes
  • 3 in conversation