- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I may run into a jam where they are not sequential. Can you help?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
If not, how do you know what the new names of each variable will be?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
https://communities.sas.com/t5/SAS-Programming/Renaming-a-group-of-variables-with-the-same-prefix/m-...
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thank you very much!