BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
hhchenfx
Barite | Level 11

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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
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. 

--
Paige Miller

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

@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.

--
Paige Miller
hhchenfx
Barite | Level 11

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

PaigeMiller
Diamond | Level 26
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. 

--
Paige Miller
hhchenfx
Barite | Level 11

Thanks a lot for your code, PaigeMiller!

HHC

BlairWhitty
Calcite | Level 5
Hi,

I am working on a similar issues, however I have a character string '_v2' that appears at the end of the variable name for some variables, but in the middle for others. Is there a way to modify this code to remove that string from anywhere in the variable name.

For example I have
id_info_v2
diary_type_v2____1 - diary_type_v2___4

Thanks!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 5528 views
  • 2 likes
  • 3 in conversation