BookmarkSubscribeRSS Feed
Bluekeys49
Obsidian | Level 7

I have a string of 50 variables named VAR_0001, VAR_0002, VAR_0003,...,VAR_0050. 

I want to rename them to abbreviated names of VR_1, VR_2, VR_3,...VR_50.  Thanks in advance for help!

8 REPLIES 8
Reeza
Super User
rename var_0001-var_0050 = vr_1-vr_50;

You can rename a series easily as above.

If your variable names are not sequential as in your example, you will need to use a different approach.

 


@Bluekeys49 wrote:

I have a string of 50 variables named VAR_0001, VAR_0002, VAR_0003,...,VAR_0050. 

I want to rename them to abbreviated names of VR_1, VR_2, VR_3,...VR_50.  Thanks in advance for help!


 

Bluekeys49
Obsidian | Level 7

I may run into a jam where they are not sequential.  Can you help?

Reeza
Super User
Is there a pattern or not?
If not, how do you know what the new names of each variable will be?
Shmuel
Garnet | Level 18

@Bluekeys49 wrote:

I may run into a jam where they are not sequential.  Can you help?


In such case you may need a more complicated method, as attached here:

data have;
     array v {*} var_0001-var_0004;
     do i=1 to 4; v(i)=i; end;
     output; 
run;

proc sql noprint;
    select name into :vlist separated by ' '
     from dictionary.columns  
     where libname='WORK' and memname='HAVE' ;
quit;
%put VLIST=&vlist;

data _null_;
  length ren_list $1200 ix $2;
  dsid = open('test');
  do i=1 to 5; /* adapt to max suffix or even bigger */
     ix = put(i,2.);
     varname = 'var_'||put(i,z4.); 
     newname = 'vr_'||strip(ix); 
     if findw("&vlist",varname) then do;
     if findw("&vlist",varname) then        
        ren_list = compbl(ren_list||varname||'='||newname||' ');
     else  put varname= ' not found';
  end;
  call symput('REN_LIST', trim(ren_list));
run;
%put REN_LIST=&ren_list;

proc datasets lib=work;
   modify have;
   rename &ren_list;
quit;
Bluekeys49
Obsidian | Level 7

From data I am working with now, yes, they are in sequential order.  However, another data set I am working with has variable names with hyphens in them (and not underscores).  Will the rename function work the same way as my original example?  For example, VAR-0001, VAR-0002, VAR-0003,...,VAR-0050.  How do I rename them to shorter version WITH underscores so that they are VR_1, VR_2, VR3,...VR50?

Reeza
Super User
rename 'var-001'n - 'var-0050'n = VR_1 - VR_50;

If you're reading multiple files that are supposed to have the same layout and then you plan to combine them, this would not be the best approach. Instead, while reading the files you should be specifying the names and types and then you can read them either all at once or all with the same specifications to make your life easier when you try and analyze the data. Or at least set the option so that you're not importing data with hyphens in the variable name. The following option ensures that spaces and hyphens are converted to underscores automatically when importing the data.

 

option validvarname = v7;

 


@Bluekeys49 wrote:

From data I am working with now, yes, they are in sequential order.  However, another data set I am working with has variable names with hyphens in them (and not underscores).  Will the rename function work the same way as my original example?  For example, VAR-0001, VAR-0002, VAR-0003,...,VAR-0050.  How do I rename them to shorter version WITH underscores so that they are VR_1, VR_2, VR3,...VR50?


 

Bluekeys49
Obsidian | Level 7

Thank you very much!  

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
  • 8 replies
  • 1324 views
  • 2 likes
  • 4 in conversation