BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

Hello

I have a data set with many columns .

I want to remove columns by the following rule:

IF name of the variables end with a number greater then 3 then need to delete this column.

So, in this example I will remove columns X4,X5, W4,W5

What is the way to do it please?

Data tbl;
input ID x1 x2 x3 x4 x5 w1 w2 w3 w4 w5 ;
cards;
1 10 20 30 40 50 11 12 13 14 15
2 20 30 40 50 60 12 13 14 15 16
;
Run;
3 REPLIES 3
Patrick
Opal | Level 21

Below one way how you could go about this.

Data tbl;
  input ID x1 x2 x3 x4 x5 w1 w2 w3 w4 w5;
cards;
1 10 20 30 40 50 11 12 13 14 15
2 20 30 40 50 60 12 13 14 15 16
;

%let droplist=;
proc sql noprint;
  select 
    name into :droplist separated by ' '
  from dictionary.columns
  where 
    libname='WORK' 
    and memname='TBL' 
    and input(scan(name,-1,,'kd'),32.)>3
  ;
quit;

data tbl_new;
  set tbl;
  drop &droplist;
run;
Kurt_Bremser
Super User

Catch the opportunity and restructure to a more usable long layout:

Data tbl;
input ID x1 x2 x3 x4 x5 w1 w2 w3 w4 w5 ;
cards;
1 10 20 30 40 50 11 12 13 14 15
2 20 30 40 50 60 12 13 14 15 16
;

data want;
set tbl;
array _x{*} x:;
array _w{*} w:;
do i = 1 to min(3,dim(_x));
  x = _x{i};
  w = _w{i};
  output;
end;
keep id i x w;
run;
ballardw
Super User
Data want;
   set have (keep=id x1-x3 w1-w3);
run;

Or were you asking because you have no idea what the actual names of the variables involved will be?

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 3 replies
  • 520 views
  • 7 likes
  • 4 in conversation