Hi Experts,
The sample of my original dataset looks as follows:
Date ar1 ar2 ar3 ar4 ar5
. | . | . | . | . | . |
. | -0.015726804 | 0.0176842945 | -0.016964348 | -0.00088675 | 0.0160770942 |
01/01/2004 | 0 | 0 | 0 | 0 | 0 |
02/01/2004 | -0.00752637 | -0.014333754 | 0.0208526907 | -0.077040716 | 0.2230172601 |
05/01/2004 | -0.072398762 | -0.064743906 | -0.026989673 | -0.127645586 | 0.0311527312 |
06/01/2004 | 0.0056040872 | 0.1373077807 | 0.0122890855 | 0.0957376211 | -0.025696325 |
07/01/2004 | 0.0310395172 | 0.036282484 | -0.00868609 | 0.0554872928 | -0.075945777 |
08/01/2004 | -0.021351471 | 0.0808676035 | -0.032316476 | 0.0035982172 | -0.042734186 |
However, I want to change the variable names. The new variables names will be
CA45245E1097 | CH0018666781 | US00208J1088 | US0025671050 | US0038301067 |
instead of ar1 ar2 ar3 ar4 ar5) and so on. Observations will remain same. Only variable names will have to be changed. How can I do that?
Since there's no discernable pattern you'll need to manually use rename statements.
Rename ar1=CA35543 ar2=CH5783 etc;
Can't we use macro for this? I see from the following link.
SAS Code Fragments: A few SAS macro programs for renaming variables dynamically
But in scenario number 1, it changed the names of only two variables. How can do that for all of my variables?
I think that this code is helpful:
%macro rename1(oldvarlist, newvarlist);
%let k=1;
%let old = %scan(&oldvarlist, &k);
%let new = %scan(&newvarlist, &k);
%do %while(("&old" NE "") & ("&new" NE ""));
rename &old = &new;
%let k = %eval(&k + 1);
%let old = %scan(&oldvarlist, &k);
%let new = %scan(&newvarlist, &k);
%end;
%mend;
data want ;
set have;
%rename1(ar1 ar2 ar3 ar4 ar5, CA45245E1097 CH0018666781 US00208J1088 US0025671050 US0038301067);
run;
But if I have ar1, ar2, ......., ar200, then what can I do? I tried with the following but it's not working.
%let oldvarlist= ar1-ar5;
%let newvarlist= CA45245E1097 CH0018666781 US00208J1088 US0025671050 US0038301067;
data want ;
set have;
%rename1(&oldvarlist, &newvarlist);
run;
I must have missed the part where you said you have 200 variables.
Do you have the name mappings somewhere?
See several options here:
sas - Create a sequence of new column names - Stack Overflow
I have names of the variables in this way:
%let newvarlist= CA45245E1097 CH0018666781 US00208J1088 US0025671050 US0038301067 ..............;
I'd recommend placing it into a dataset instead and using the answer from the link above. You wont need the first proc sql but the rest should be pretty much the same.
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.