The DROP statement cannot be run conditionally. You need to conditionally generate the DROP statement (or DROP= dataset option).
To use a trivial example dataset let's start with SASHELP.CLASS
and split it into individual datasets. Note that this dataset only has one observation per NAME, but I will add BY group processing to the code generation step so you can see how you could use it in the case where there are multiple observations per name.
First let's generate code for single DATA statement that creates multiple output datasets. Based on the value of the SEX variable it will conditionally add a DROP= dataset option.
filename code temp;
data _null_;
set sashelp.class end=eof ;
by name ;
file code ;
if _n_=1 then put 'data' ;
if first.name then do;
put ' ' name @ ;
if sex='F' then put '(drop=age)' @ ;
put ;
end;
if eof then put ';' ;
run;
Now let's append the code for the rest of the data step that will read the source dataset and output the records to the appropriate dataset.
data _null_;
set sashelp.class end=eof ;
by name ;
file code mod ;
if _n_=1 then put ' set sashelp.class; ' ;
if first.name then put ' if name =' name $quote. 'then output ' name ';' ;
if eof then put 'run;' ;
run;
Finally run the generated code.
%include code / source2 ;