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
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
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.
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
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.
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
Code worked marvelously! Thank you.
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!
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.
Ready to level-up your skills? Choose your own adventure.