Hi,
I have a dataset where a number of variables end in "_2019".
I would like to remove the suffix.
The problem is that the name has multiple _
Any help is very much appreciated?
HHCFX
data have;
input id_ab_x var_1 s_01_03_2019 xy_2_05_2019;
datalines;
1 2 1 1
2 2 3 4
;run;
proc contents data=have out=_contents_ noprint;
run;
proc sql noprint;
select cats(name,'=',tranwrd(name,'_2019','')) into :renames
separated by ' ' from _contents_ where find(name,'_2019')>0;
quit;
%put &=renames;
proc datasets library=work nolist;
modify have;
rename &renames;
delete _contents_;
run;
quit;
This code will fail if the text '_2019' appears elsewhere (not at the end of the variable name) in the variable names.
@hhchenfx wrote:
Hi,
I have a dataset where a number of variables end in "_2019".
I would like to remove the suffix.
The problem is that the name has multiple _
Any help is very much appreciated?
HHCFX
data have;
input id_ab_x var_1 s_01_03_2019 xy_2_05_2019;
datalines;
1 2 1 1
2 2 3 4
;run;
I'm not sure I understand the problem. In your code, you can simply delete the parts you don't want. In bulk, you can delete the text _2019 from wherever it is found, using the Edit->Replace menu command.
If your problem is really that you have existing data sets with dozens/hundreds/thousands of variable names that you need to change (which is certainly not what you stated), then we'd need to know a lot more about the problem. And we'd also need to know if you are delete only "_2019" from the variable name, or if you want to delete the LAST text after the last underscore (and delete that last underscore as well). In other words, we need an exact problem description clearly delineating what needs to be deleted.
Thanks for asking.
I have new data each day and there are 30+ variables ending with _2019.
Names are different from time to time so I need to clear that part by SAS.
HHC
proc contents data=have out=_contents_ noprint;
run;
proc sql noprint;
select cats(name,'=',tranwrd(name,'_2019','')) into :renames
separated by ' ' from _contents_ where find(name,'_2019')>0;
quit;
%put &=renames;
proc datasets library=work nolist;
modify have;
rename &renames;
delete _contents_;
run;
quit;
This code will fail if the text '_2019' appears elsewhere (not at the end of the variable name) in the variable names.
Thanks a lot for your code, PaigeMiller!
HHC
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.