how to drop a series of variables in SAS

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 128
Accepted Solution

how to drop a series of variables in SAS

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

How can I do this efficiently? Thanks


Accepted Solutions
Solution
‎01-16-2014 10:37 AM
Super Contributor
Posts: 1,636

Re: how to drop a series of variables in SAS

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


All Replies
Super Contributor
Posts: 1,636

Re: how to drop a series of variables in SAS

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

drop f:;

Frequent Contributor
Posts: 128

Re: how to drop a series of variables in SAS

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

Solution
‎01-16-2014 10:37 AM
Super Contributor
Posts: 1,636

Re: how to drop a series of variables in SAS

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;

New Contributor
Posts: 2

Re: how to drop a series of variables in SAS

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;

Super Contributor
Posts: 1,636

Re: how to drop a series of variables in SAS

Posted in reply to kmoonmurr

have you tried your code?

New Contributor
Posts: 2

Re: how to drop a series of variables in SAS

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.

Super Contributor
Posts: 1,636

Re: how to drop a series of variables in SAS

Posted in reply to kmoonmurr

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

PROC Star
Posts: 7,492

Re: how to drop a series of variables in SAS

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;

Occasional Contributor
Posts: 6

Re: how to drop a series of variables in SAS

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;

Super Contributor
Posts: 276

Re: how to drop a series of variables in SAS

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

New Contributor
Posts: 3

Re: how to drop a series of variables in SAS

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,

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

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