SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
GS2
Obsidian | Level 7 GS2
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

 

View solution in original post

11 REPLIES 11
Reeza
Super User

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


 

yabwon
Onyx | Level 15

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



ballardw
Super User

@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.

PaigeMiller
Diamond | Level 26

@ballardw wrote:

It might help though to explain why this is needed. Perhaps we can present another solution or prevention tip.


Yes, I agree. @GS2, why is this needed? Its hard for me to imagine this being of any benefit in any programming I have done.

--
Paige Miller
GS2
Obsidian | Level 7 GS2
Obsidian | Level 7
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
Reeza
Super User

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

 

PaigeMiller
Diamond | Level 26

@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
PaigeMiller
Diamond | Level 26

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
Tom
Super User Tom
Super User

@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?

 

ballardw
Super User

@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.

 

 

 

 

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 11 replies
  • 3163 views
  • 9 likes
  • 7 in conversation