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;
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;
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;
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?
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.
Ready to level-up your skills? Choose your own adventure.