JohnD91
JohnD91

Reputation: 583

Is there any way to apply conditional logic for keeping/dropping columns in SAS?

I'm trying to drop particular columns in SAS based on a macro variable, my hands are slightly tied in terms of what code I can use - so I need a solution in BASE SAS.

I've already tried wrapping the drop/keep in an if, but I know the drop happens at run time so this won't work.

Example:

data dropsomecolumns;

  if &somemacro =1 then do;
    drop somecol1 somecol2;
  end;

run;

Upvotes: 0

Views: 365

Answers (2)

Tom
Tom

Reputation: 51621

You either need to use macro code to conditionally generate the code you want.

data dropsomecolumns;
  set have;
%if &somemacro =1 %then %do;
  drop somecol1 somecol2;
%end;
run;

Or change so that the macro variable has the list of columns to drop.

%let drop_columns=somecol1 somecol2;
data dropsomecolumns(drop=&drop_columns);
  set have;
run;

Note that the drop statement will give a warning if no variables are listed, but the drop= dataset option will not give that warning.

Upvotes: 3

Reeza
Reeza

Reputation: 21294

You have have no variables listed and it will work fine and assumes not are to be dropped. So if the macro variable is a list of variables just use:

 drop &drop_columns;

This works fine:

data demo;
    set sashelp.class;
    drop ;
run;

So no conditional logic needed.

Upvotes: 0

Related Questions