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!
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!
I may run into a jam where they are not sequential. Can you help?
@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;
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?
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?
Thank you very much!
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.