Suppose I have an array with inconsistent variables.
For example I have:
an array with dim = 3, and the variables are called "brand", "model", "type"
Is there a way for me to create another array which renames these variables (whilst retaining the original variables).
In my head, I'm trying something like this:
data want;
set have;
array to_rename {3} revenue expenses assets;
array new {3} newvar1-newvar3;
array new_name {3} newnames1-newnames3;
DO i = 1 to 3;
new(i) = to_rename(i)/USD;
new_name(i) = cats(new(i),'_USD');
END;
DROP I;
RUN;
So, the output would be an additional 3 variables, entitled:
revenue_USD, expenses_USD, assets_USD
Thanks 🙂
If names are inconsistent and you already know the target of rename you can do:
data want;
merge have
have(rename=(revenue=revenue_usd
expenses=expenses_usd
assets=assets_usd));
by;
run;
Here is an example which you can try
However in arrays we need to take care that we can use only either all numeric variables or all character variables.
In the below example i used all numeric variables to rename.
proc sql;
select cats(name,'_USD') into: names separated by ' ' from dictionary.columns where libname='SASHELP' and memname='CLASS' and type='num';
quit;
%put &names;
data class;
set sashelp.class;
array vars(*) age -- weight;
array vars2(*) &names;
do i = 1 to dim(vars);
vars2(i)=vars(i);
end;
run;
You may want to reconsider the whole "add any suffix" idea to begin with.
When you leave variables with a numeric suffix then you can use all of the LIST shortcuts such as var1 - var3. As soon as you add a suffix then you will always have to list var1_usd var2_usd var3_usd.
I might suggest that you name them Usd_1 Usd_2 and Usd_3. Use a LABEL to describe them back to something else.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.