Is there a way to remove columns that does not end with "_22" from a data set? Moreover, is there a way to remove "_22" suffix from column names from another data set?
Thank you in advance
proc sql noprint;
select catx("=",name,substr(name,1,length(name)-3)), substr(name,1,length(name)-3)
into :renamestr separated by " ", dropstr separated by " "
from dictionary.columns
where libname = "WORK" and memname = "HAVE" and substr(name,length(name)-2) = "_22";
quit;
data want;
set have;
drop &dropstr.;
rename &renamestr.;
run;
Since you want to remove variables, PROC DATASETS can't do it, a DATA step is needed. My bad.
In PROC SQL, you can retriev variable names from DICTIONARY.COLUMNS. With SELECT INTO, you can create macro variables which contain the necessary lists for DROP and RENAME statements used in PROC DATASETS.
proc sql noprint;
select catx("=",name,substr(name,1,length(name)-3)), substr(name,1,length(name)-3)
into :renamestr separated by " ", dropstr separated by " "
from dictionary.columns
where libname = "WORK" and memname = "HAVE" and substr(name,length(name)-2) = "_22";
quit;
data want;
set have;
drop &dropstr.;
rename &renamestr.;
run;
Since you want to remove variables, PROC DATASETS can't do it, a DATA step is needed. My bad.
What ways do you know (if any) to get the names of variables in your data set?
In my occasionally humble opinion, the question is why to do you have variables with names you don't want in the first place. Especially something ending in _22. That almost implies that you have 21 other names that are acceptable (ending in _1 _2 _3 ... _21).
When you have data set names you don't like and want different names then Proc Datasets will let you rename the variables in place.
Proc datasets library=Somelib; modify adataset; rename var_22 = var thatvar_22 = thatvar ; run; quit;
The example code will modify the dataset named Adataset in the library named Somelib by renaming tow variables.
Note this procedure uses a QUIT statement to end the procedure.
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
For SAS newbies, this video is a great way to get started. James Harroun walks through the process using SAS Studio for SAS OnDemand for Academics, but the same steps apply to any analytics project.
Find more tutorials on the SAS Users YouTube channel.