BookmarkSubscribeRSS Feed
gahlot1999
Fluorite | Level 6

I want to create a macro to rename all variables in sashelp.cars with suffix 1.

For eg. MODEL will be MODEL1.

PS: I Don't want to use renaming (eg. rename model = model1). I want to find count of variable in the dataset sashelp.cars and use do loop and then rename inside macro (also don't want to use variable name directly while renaming).

Any easy approach to do this?

3 REPLIES 3
ChrisNZ
Tourmaline | Level 20

You want to rename with using the RENAME statement or option? And without using the variable name?

This makes no sense at all.

 

I want to create a macro to rename all variables in sashelp.cars with suffix 1.

For eg. MODEL will be MODEL1.

PS: I Don't want to use renaming (eg. rename model = model1). I want to find count of variable in the dataset sashelp.cars and use do loop and then rename inside macro (also don't want to use variable name directly while renaming).

Any easy approach to do this?

PaigeMiller
Diamond | Level 26

@gahlot1999 wrote:

 

PS: I Don't want to use renaming (eg. rename model = model1). I want to find count of variable in the dataset sashelp.cars and use do loop and then rename inside macro (also don't want to use variable name directly while renaming).

Any easy approach to do this?


Why this restriction? Why are you saying I can only use certain parts of SAS to do this and not other parts of SAS?

 

You realize that you can't rename variables in SASHELP.CARS because users do not have WRITE permission to any SASHELP data sets. You could copy the data set into the WORK library.

 

Anyway, this is easily done without a macro and without specifying an explicit loop, but with the RENAME statement and with a single macro variable.

--
Paige Miller
Kurt_Bremser
Super User

No macro needed, only a macro variable.

proc sql noprint;
select trim(name) !! "=" !! trim(name) !! "1" into :renames separated by " "
from dictionary.columns
where libname = "SASHELP" and memname = "CARS";
quit;

%put &renames.;

The resulting macro variable can be used in a RENAME statement or RENAME= dataset option.

sas-innovate-white.png

Missed SAS Innovate in Orlando?

Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.

 

Register now

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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