BookmarkSubscribeRSS Feed
UniversitySas
Quartz | Level 8

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 🙂

3 REPLIES 3
Shmuel
Garnet | Level 18

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;

 

Jagadishkatam
Amethyst | Level 16

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;

 

Thanks,
Jag
ballardw
Super User

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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 1324 views
  • 0 likes
  • 4 in conversation