SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

Drop a variable if exits

Reply
Super Contributor
Posts: 704

Drop a variable if exits

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

Occasional Contributor
Posts: 9

Re: Drop a variable if exits

[ Edited ]

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!

PROC Star
Posts: 1,564

Re: Drop a variable if exits

[ Edited ]

@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);

Ask a Question
Discussion stats
  • 2 replies
  • 550 views
  • 4 likes
  • 3 in conversation