BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ken_oy
Fluorite | Level 6

I want to drop/delete F1_flag, F2_flag, F3_flag, .... F99_flag from my dataset.

How can I do this efficiently? Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Linlin
Lapis Lazuli | Level 10

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;

View solution in original post

11 REPLIES 11
Linlin
Lapis Lazuli | Level 10

if you don't have other varaibles staring with "f" you could use:

drop f:;

Ken_oy
Fluorite | Level 6

Yes, that is the problem. I do have F1, F2, ... in my dataset.

Linlin
Lapis Lazuli | Level 10

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;

kmoonmurr
Fluorite | Level 6

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;

Linlin
Lapis Lazuli | Level 10

have you tried your code?

kmoonmurr
Fluorite | Level 6

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.

Linlin
Lapis Lazuli | Level 10

It doesn't work for me. please post your code and log file.

art297
Opal | Level 21

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;

mikki
Calcite | Level 5

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;

kuridisanjeev
Quartz | Level 8

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

deval
Calcite | Level 5

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,

sas-innovate-2024.png

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.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 11 replies
  • 7093 views
  • 6 likes
  • 7 in conversation