BookmarkSubscribeRSS Feed
Ronein
Onyx | Level 15

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?

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 831 views
  • 7 likes
  • 4 in conversation