BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
NewUsrStat
Lapis Lazuli | Level 10

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

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User
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.

View solution in original post

5 REPLIES 5
Kurt_Bremser
Super User

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.

NewUsrStat
Lapis Lazuli | Level 10
Thank you kurt_Bremser. Is there an example or something like that to inspire to?
Kurt_Bremser
Super User
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.

Astounding
PROC Star

What ways do you know (if any) to get the names of variables in your data set?

ballardw
Super User

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.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 1330 views
  • 1 like
  • 4 in conversation