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

Hi there

 

Can anyone help me on renaming all variables starting with the same preffix to a different prefix?

 

Cheers

 

Sue

1 ACCEPTED SOLUTION

Accepted Solutions
gamotte
Rhodochrosite | Level 12

Hello,

 

data have; 
    input a_a a_b a_1 a_2 a_1000;
    cards;
1 2 3 4 5
;
run;

data _NULL_;
    call execute('data want; set have; rename');

    do until(fend);
        set sashelp.vcolumn end=fend;
        where libname="WORK" and memname="HAVE" and NAME like 'a_%';
        call execute(cats(NAME,'=b_', substr(NAME,3)));
    end;

call execute('; run;');
stop;
run;

View solution in original post

12 REPLIES 12
PaigeMiller
Diamond | Level 26

Rename x1-x100 to be y1-y100.

 

In a SAS DATA step, use:

 

rename x1-x100=y1-y100;

 

 

--
Paige Miller
Suzy_Cat
Pyrite | Level 9

Hi Paige,

 

Thanks for the suggestions. It is working when I tried below:

rename Cat_:=Sohi_:;

 

the name of those datasets is not ending with digits and they are random combination 

PaigeMiller
Diamond | Level 26

@Suzy_Cat wrote:

Hi Paige,

 

Thanks for the suggestions. It is working when I tried below:

rename Cat_:=Sohi_:;


Did you mean to say that it is NOT working??

--
Paige Miller
Suzy_Cat
Pyrite | Level 9

Hi Paige , yes you were right. did not realize there was a type while I actually meant it not working as expected.

 

Thanks

Tom
Super User Tom
Super User

It is impossible for that syntax to work given how the : suffix works in variable lists.  When you use the : suffix you are asking SAS to list all variable in the current data step that start with that prefix.  So when you use it on the right of the equal sign in a RENAME statement it will fail in two ways.  Either it won't find the variables since they haven't been defined yet. Or it will find the variables, in which case they cannot be used as the new names since they already exist.

gamotte
Rhodochrosite | Level 12

Hello,

 

data have; 
    input a_a a_b a_1 a_2 a_1000;
    cards;
1 2 3 4 5
;
run;

data _NULL_;
    call execute('data want; set have; rename');

    do until(fend);
        set sashelp.vcolumn end=fend;
        where libname="WORK" and memname="HAVE" and NAME like 'a_%';
        call execute(cats(NAME,'=b_', substr(NAME,3)));
    end;

call execute('; run;');
stop;
run;

ScottBass
Rhodochrosite | Level 12

@Suzy_Cat wrote:

Hi there

 

Can anyone help me on renaming all variables starting with the same preffix to a different prefix?

 

Cheers

 

Sue


 

@gamotte 's solution works fine, but would perform poorly if the target dataset was large.

 

Here is another approach:

 

data have;
   length foo a_a a_b x_y x_z a_c bar blah 8;
run;

proc sql noprint;
   select name into :vars separated by " "
   from dictionary.columns
   where libname="WORK" 
     and memname="HAVE"
     and name like "a_%"
   ;
quit;

%put &=vars;

%macro code;
&word=newprefix_%scan(&word,2,_)
%mend;

proc datasets lib=work nolist;
   modify have;
   rename %loop(&vars);
quit;

https://github.com/scottbass/SAS/blob/master/Macro/loop.sas


Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.
PaigeMiller
Diamond | Level 26

I'm glad you said that @ScottBass. Rename using PROC DATASETS is preferable to rename in a DATA step.

 

Even more compact code, with no macro looping

proc sql noprint;
   select cats(name,'=',tranwrd(name,'a_','b_')) into :renames separated by " "
   from dictionary.columns
   where libname="WORK" 
     and memname="HAVE"
     and name eqt "a_"
   ;
quit;
proc datasets library=work nolist;
    modify have;
    rename &renames;
quit;
  
--
Paige Miller
ScottBass
Rhodochrosite | Level 12

@PaigeMiller Yep your code is more compact.  I find my %loop macro so useful (to me anyway) that I may have a bit of tunnel vision.


Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.
PaigeMiller
Diamond | Level 26

@ScottBass you don't even show your %loop macro, so your code above won't work for anyone but you.

--
Paige Miller
Suzy_Cat
Pyrite | Level 9
You guys are awesome! provided more effective ways. good learning! Thanks a lot 🙂

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
  • 12 replies
  • 10431 views
  • 12 likes
  • 5 in conversation