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,
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.