I want to drop/delete F1_flag, F2_flag, F3_flag, .... F99_flag from my dataset.
How can I do this efficiently? Thanks
example:
data have;
input a1_flag a2_flag a3_flag a1 a2;
cards;
1 2 3 4 5
;
proc sql noprint;
select name into : dropped separated by ' '
from dictionary.columns
where libname='WORK' and memname='HAVE' and upcase(name) contains "FLAG";
quit;
data want;
set have;
drop &dropped;
proc print;run;
if you don't have other varaibles staring with "f" you could use:
drop f:;
Yes, that is the problem. I do have F1, F2, ... in my dataset.
example:
data have;
input a1_flag a2_flag a3_flag a1 a2;
cards;
1 2 3 4 5
;
proc sql noprint;
select name into : dropped separated by ' '
from dictionary.columns
where libname='WORK' and memname='HAVE' and upcase(name) contains "FLAG";
quit;
data want;
set have;
drop &dropped;
proc print;run;
If those flag variables were created in order, then you can delete with one simple statement:
drop F1_flag -- F99_flag;
To find out if they are in order, do
proc contents position ;
run;
have you tried your code?
Yup. I do it all the time but I did create a simple data set before I sent this reply to you just to be sure. Works.
It doesn't work for me. please post your code and log file.
Key_oy: One problem with Linlin's suggested code, which may or may not be an issue with your data, is that it will drop any variable that contains the string flag. E.g., in a data set like:
data have;
input a1_flag a2_flag a3_flag a1 a2 _flag_I_want;
cards;
1 2 3 4 5 6
;
it would also drop flag_I_want. The following is a way around that, namely just to drop variables that end with _flag:
proc sql noprint;
select name into : dropped separated by ' '
from dictionary.columns
where libname='WORK' and
memname='HAVE' and
reverse(upcase(trim(name))) like "GALF_%"
;
quit;
data want;
set have;
drop &dropped;
run;
Hi Ken,
See the following code that drops only the variables starting with a and ending with flag. I updated Linlin's code with like and 'A%FLAG'.
data have;
input a1_flag a2_flag a3_flag a1 a2;
cards;
1 2 3 4 5
;
proc sql noprint;
select name into : dropped separated by ' '
from dictionary.columns
where libname='WORK' and memname='HAVE' and upcase(name) like 'A%FLAG';
quit;
data want;
set have;
drop &dropped;
proc print;run;
Hi,,
If your variable names are contiguous,then you can simply use like bellow...
Data test;
input f1 f2 f3 f4 f5 f6 n1 n2;
cards;
1 2 3 4 5 6 8 9
;
run;
Data yy;
set y;
drop f1-f6;
run;
Proc print;run;
Thanks & Regards,
Sanjeev.K
Hi,
If you drop the variables using drop option rather than drop statement, than it will be more efficient way.
data abc ;
set xyz (drop = &dropped.);
run;
Thanks,
⏰
Today is the last day to save with the early bird rate! Register today for just $695 - $100 off the standard rate.
Plus, pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.