DATA Step, Macro, Functions and more

Delete column whose name certain characters

Accepted Solution Solved
Reply
Super Contributor
Posts: 503
Accepted Solution

Delete column whose name certain characters

Hi Everyone,

I want to delete all columns that have name with string "ID" in it. 

My data is like that

 

data have;
input abIc ID1 bxI4D b_ID2 xyzID_3;
cards;
1 2 3 4 5
;run;

 

So I only keep abIc and bxI4D.

 

Thank you for your help.

 

HC


Accepted Solutions
Highlighted
Solution
‎01-13-2018 05:06 PM
PROC Star
Posts: 1,294

Re: Delete column whose name certain characters

Posted in reply to novinosrin
data have;
input abIc ID1 bxI4D b_ID2 xyzID_3; 
cards;
1 2 3 4 5
;run;

proc sql;
select name into :drop_list separated by ','
from sashelp.vcolumn
where libname="WORK" and memname="HAVE" and index(name,'ID')>0;
quit;

%put &drop_list;

proc sql;
   alter table have
      drop  &drop_list;
quit;

View solution in original post


All Replies
Super User
Posts: 22,820

Re: Delete column whose name certain characters

Not easily but you can build a list of variables from either PROC CONTENTS or SASHELP.VCOLUMN which has the list of variables. 

Then include those in your DROP statement. 

PROC Star
Posts: 1,294

Re: Delete column whose name certain characters

data have;
input abIc ID1 bxI4D b_ID2 xyzID_3; 
cards;
1 2 3 4 5
;run;

proc sql;
select name into :drop_list separated by ' '
from sashelp.vcolumn
where libname="WORK" and memname="HAVE" and index(name,'ID')>0;
quit;

%put &drop_list;

data have;
set have;
drop &drop_list;
run;

Highlighted
Solution
‎01-13-2018 05:06 PM
PROC Star
Posts: 1,294

Re: Delete column whose name certain characters

Posted in reply to novinosrin
data have;
input abIc ID1 bxI4D b_ID2 xyzID_3; 
cards;
1 2 3 4 5
;run;

proc sql;
select name into :drop_list separated by ','
from sashelp.vcolumn
where libname="WORK" and memname="HAVE" and index(name,'ID')>0;
quit;

%put &drop_list;

proc sql;
   alter table have
      drop  &drop_list;
quit;
Super Contributor
Posts: 503

Re: Delete column whose name certain characters

Posted in reply to novinosrin

Thanks a lot.

HHC

Super Contributor
Posts: 503

Re: Delete column whose name certain characters

Posted in reply to novinosrin

So weird! 

SAS only take all letter in CAP to run that code.

 

Here is the log:

 

3291 proc sql;
3292 select name into :drop_list separated by ','
3293 from sashelp.vcolumn
3294 where libname="WORK" and memname="myfile" and index(name,'ID')>0;
NOTE: No rows were selected.
3295 quit;
NOTE: PROCEDURE SQL used (Total process time):
real time 0.03 seconds
cpu time 0.03 seconds


3296
3297 proc sql;
3298 select name into :drop_list separated by ','
3299 from sashelp.vcolumn
3300 where libname="WORK" and memname="MYFILE" and index(name,'ID')>0;
3301 quit;
NOTE: PROCEDURE SQL used (Total process time):
real time 0.03 seconds
cpu time 0.00 seconds

PROC Star
Posts: 1,294

Re: Delete column whose name certain characters

memname is always stored as UPCASE in metadata

PROC Star
Posts: 8,098

Re: Delete column whose name certain characters

[ Edited ]
Posted in reply to novinosrin

@novinosrin: Maybe that's true for Windows, but not for all operating systems. That's why I suggested the find function using the 'i' modifier.

 

Oops! Just read your post more carefully! Yes, libname and memname have to be in upper case. However, the variable names are case sensitive on some operating systems.

 

Art, CEO, AnalystFinder.com

 

PROC Star
Posts: 8,098

Re: Delete column whose name certain characters

 

Same solution as @novinosrin but, if case is irrelevant, I'd suggest using FIND function rather than INDEX function.

 

data have;
  input abIc id1 bxI4D b_ID2 xyzID_3; 
  cards;
1 2 3 4 5
;run;

proc sql noprint;
  select name into :drop_list separated by ' '
  from sashelp.vcolumn
    where libname="WORK" and memname="HAVE" and find(name,'ID','i');
quit;

data want;
  set have;
  drop &drop_list;
run;

Art, CEO, AnalystFinder.com

 

☑ This topic is solved.

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

Discussion stats
  • 8 replies
  • 136 views
  • 4 likes
  • 4 in conversation