BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
DrBigAl
Fluorite | Level 6

Hello Community!

 

I am trying to replace SAS variable names for a dataset that has been loaded to the server. For instance, SAS dataset (Var1, Var2, Var3, Var4, etc.). Need to change Var"" to: CustomerNo., CustName, Product, Sale, etc.. No need to do import because the table is already available in on the server.

 "abc.filename"

 

This is what I have. Where have I gone wrong?

Actual

Var1          Var2          Var3          Var4

123            Ted           Knife          $25

124            Bill            Watch        $55

 

Need

CustomerNo          CustName            Product          Sale

123                         Ted                       Knife               $25

124                         Bill                        Watch             $55

 

data FactSetNames;

     input FileNames $30.;

     datalines;

 

CustomerNo

CustName

Product

Sale 

     ;

run;

 

%macro FactSetloop;

     %do i = 1 %to 255;

           data _NULL_;

                set FactSetNames;

                if _N_ = &i then call symputx("FileName",FileNames,'G');

           run;

 

          Data step?

 

           run;

     %end;

%mend;

%FactSetloop

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

If you already have the factsetnames file then there isn't anything tedious about the code at all. Here is the same code, but which uses the already created factsetnames file. It would accomplish the renaming for all of the variables in the factsetnames file:

data _null_;
  length forexec $255;
  set FactSetNames end=eof;
  if _n_ eq 1 then call execute ("data Need; set Actual;");
  forexec=catt("rename var",_n_,"=",FileNames,";");
  call execute(forexec);
  if eof then call execute ("run;");
run;

Art, CEO, AnalystFinder.com

 

 

View solution in original post

7 REPLIES 7
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Why are they being loaded in as var1 var2 in the first place?  Fix the broken part, don't try to repair it after the fact.  I suspect its likely your importing a spreadsheet.  Bin that, import a csv and write a datastep to do the process.  That way you can read in the data you want, set labels, names and lengths etc. at load time.

art297
Opal | Level 21

Here is one way:

data _null_;
  length forexec $255;
  input FileNames $30.;
  if _n_ eq 1 then call execute ("data Need; set Actual;");
  forexec=catt("rename var",_n_,"=",FileNames,";");
  call execute(forexec);
  if _n_ eq 4 then call execute ("run;");
  datalines;
CustomerNo
CustName
Product
Sale 
;

Art, CEO, AnalystFinder.com

 

Astounding
PROC Star

Where did you go wrong?  By starting to write a program.  First, you should have read related pieces of the manual, so you would have an idea of what the program should look like.  Some topics:

 

The RENAME statement

The RENAME= option on a SET statement

The MODIFY statement within PROC DATASETS

 

Once you have some idea of what the program should look like, you can begin the programming.  One related hint:  you would need two variables in a SAS data set, for example, OLDNAME and NEWNAME.  They should both be character, 32 characters long (maximum length for a variable name in SAS).  A single variable (FileNames) is not enough.  So when OLDNAME is Var1, NEWNAME would be CustomerNo.

DrBigAl
Fluorite | Level 6
The dataset was loaded by another person. I only have a seperate variable name key. Don't have access to the original file
DrBigAl
Fluorite | Level 6
Thank you for your solution. The problem is, I forgot to state, is that there are 150 variable names which doing each one is tedious and time consuming.
art297
Opal | Level 21

If you already have the factsetnames file then there isn't anything tedious about the code at all. Here is the same code, but which uses the already created factsetnames file. It would accomplish the renaming for all of the variables in the factsetnames file:

data _null_;
  length forexec $255;
  set FactSetNames end=eof;
  if _n_ eq 1 then call execute ("data Need; set Actual;");
  forexec=catt("rename var",_n_,"=",FileNames,";");
  call execute(forexec);
  if eof then call execute ("run;");
run;

Art, CEO, AnalystFinder.com

 

 

DrBigAl
Fluorite | Level 6

Code worked marvelously! Thank you.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 7 replies
  • 2317 views
  • 1 like
  • 4 in conversation