- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hello,
I have 500 variables in a dataset (some character and numeric) and I want to rename them all sequentially as var1-var500. Is there an efficient way to do this? Thank you
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
There are many other ways to refer to variable lists. Here is a reference that illustrates how to refer to variables and datasets in a short cut list:
https://blogs.sas.com/content/iml/2018/05/29/6-easy-ways-to-specify-a-list-of-variables-in-sas.html
@GS2 wrote:
I am writing an array to change the variables from yes/no to 1/0 and wanted to sequentially call the variables instead of listing all of them
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
This suggestion works well
https://communities.sas.com/t5/SAS-Programming/Rename-all-variables/m-p/500828#M133408
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
SASHELP.VCOLUMN + PROC DATASETS + CALL EXECUTE
1. Gets you the list of variables in the order
2. Renames your variables
3. Controls the code overall.
Replace the two occurences of myDataset with your data set name and the appropriate library name.
data _new_list;
set sashelp.vtable
(where=(upcase(libname)='WORK' and upcase(memname) = "MYDATASET")) end=eof;
*filter list for only tables of interest;
*start proc datasets;
if _n_=1 then
call execute ('proc datasets lib=WORK nodetails nolist; modify myDataset; rename ');
*add on new name calculation;
new_name=catt('Name', put(_n_, z3.));
*pass new and old name to proc datasets;
call execute (name);
call execute ('=');
call execute (new_name);
*if last record then quit;
If eof then
call execute (';run;quit;');
run;
@GS2 wrote:
Hello,
I have 500 variables in a dataset (some character and numeric) and I want to rename them all sequentially as var1-var500. Is there an efficient way to do this? Thank you
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
try this:
data have;
a=1;
b=2;
c="3";
d="4";
run;
title "Before";
proc print data = have;
run;
proc transpose data = have(obs=0) out = tmp(keep = _name_);
var _all_;
run;
proc sql;
select _name_ || ' = var' || put(monotonic(), best. -L)
into :renameList separated by " "
from tmp;
run;
proc datasets lib = work nodetails nolist noprint;
modify have;
rename &renameList.;
run;
quit;
title "After";
proc print data = have;
run;
title;
Bart
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug
"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings
SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation
- Tags:
- try this:
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@GS2 wrote:
Hello,
I have 500 variables in a dataset (some character and numeric) and I want to rename them all sequentially as var1-var500. Is there an efficient way to do this? Thank you
Yes, but it will involve getting the list of existing variables in the order you want first.
Example of code that you would want:
proc datasets library=somelib; modify datasetname; rename thisvar=var1 thatvar=var2 othervar=var3 ; run; quit;
If you want to rename them in the order they currently appear in the data set you can get the list of variables and the order a couple of ways to create the new var. An example you can examine results:
proc contents data=sashelp.class out=work.classlist noprint; run; data want; set work.classlist (keep= name varnum); length newvar $ 32; newvar= catt('var',varnum);
file print;
put name '=' newvar;
run;
The data step will write to the results window a list of names that you could use in the example rename. Use your data set library and name of course to make sense for your purpose.
If you need the variables renamed in another then you will need to describe what that order must be.
Hint: if you cant get the example work.classlist to sort as desired this is going to be a bit tricky and may require just writing the 500 rename pairs by hand.
It might help though to explain why this is needed. Perhaps we can present another solution or prevention tip.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
There are many other ways to refer to variable lists. Here is a reference that illustrates how to refer to variables and datasets in a short cut list:
https://blogs.sas.com/content/iml/2018/05/29/6-easy-ways-to-specify-a-list-of-variables-in-sas.html
@GS2 wrote:
I am writing an array to change the variables from yes/no to 1/0 and wanted to sequentially call the variables instead of listing all of them
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@GS2 wrote:
I am writing an array to change the variables from yes/no to 1/0 and wanted to sequentially call the variables instead of listing all of them
So @Reeza beat me to it, but I'll say it anyway. None of this variable name changing is necessary.
If your variable names appear in the data set as AARDVARK ANTELOPE ARMADILLO ... ZEBRA, you don't have to type them all out, and you don't have to rename them to have names with consecutive numbers. You can refer to them all with the double-dash, such as
array animals(*) aardvark--zebra;
or if the order is not alphabetical, they appear in your data set in some other order, such as GORILLA LION ZEBRA AARDVARK ... NARWHAL
then you refer to them in the order they appear in your data set as
array animals(*) gorilla--narwhal;
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Okay, I just did a search, and this is the first ever use of the word "aardvark" in the SAS Communities.
I'm giving myself a gold star, and taking the rest of the day off! 😁🙌👍
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@GS2 wrote:
I am writing an array to change the variables from yes/no to 1/0 and wanted to sequentially call the variables instead of listing all of them
If you don't want to actually change the names then just use an array. Since you mentioned that you had numeric and character variables then perhaps two arrays?
data want ;
set have ;
array _c _character_;
array _n _numeric_;
do over _c;
if upcase(_c) in ('YES','Y','TRUE') then _c='YES';
else _c='NO';
end;
do over _n;
_n = (_n > 0);
end;
run;
Or perhaps leave the variables as is and just use a format to display the values as YES or NO?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@GS2 wrote:
I am writing an array to change the variables from yes/no to 1/0 and wanted to sequentially call the variables instead of listing all of them
Another option might be to go back to how you read the data. You can create a custom informat that would read "yes" and "no" as 1 and 0. Read the data with the new informat.
proc format; invalue yesno (upcase) 'YES' = 1 'NO' = 0 other = . ; data example; informat x y z yesno.; input x y z; datalines; Yes no nO yes NO No yES yEs yeS ;
The upcase option on the invalue statement uses upper case text to compare to the values so minor issues like inconsistent capitalization are treated as, usually, desired for reading.