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.

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
  • 634 views
  • 0 likes
  • 4 in conversation