BookmarkSubscribeRSS Feed
tutu_
Fluorite | Level 6

I have a dataset like this, the dataset name is data1

PIN      data1_S00                 data1_ZC00

1               xxx                                     xxx

2               xxx                                     xxx

 

I want to rename the variable if the variable name contains "S00" or "S96" or "S97"

Here is the code I wrote, it does the job but it does a lot of unnecessary computation.

The warning is like this "ERROR: Variable s805_S96 is not on file WORK.WANT", This variable is not even in the dataset, I don't know how to get rid of the warning.

 

%macro rename(data1);

data want_new; set want;run;

proc sql noprint ;
select cats(name,'=&data1') into :renames1 separated by ' '
from dictionary.columns
where libname='WORK' and memname='WANT_NEW' and upcase(name) like '%_S00%' or upcase(name) like '%_S96%';
;
quit;

proc datasets nolist library=work;
modify want_new;
rename &renames1;
run;
quit;

%mend;

%rename(data1);

2 REPLIES 2
ballardw
Super User

@tutu_ wrote:

I have a dataset like this, the dataset name is data1

PIN      data1_S00                 data1_ZC00

1               xxx                                     xxx

2               xxx                                     xxx

 

I want to rename the variable if the variable name contains "S00" or "S96" or "S97"

Here is the code I wrote, it does the job but it does a lot of unnecessary computation.

The warning is like this "ERROR: Variable s805_S96 is not on file WORK.WANT", This variable is not even in the dataset, I don't know how to get rid of the warning.

 

%macro rename(data1);

data want_new; set want;run;

proc sql noprint ;
select cats(name,'=&data1') into :renames1 separated by ' '
from dictionary.columns
where libname='WORK' and memname='WANT_NEW' and upcase(name) like '%_S00%' or upcase(name) like '%_S96%';
;
quit;

proc datasets nolist library=work;
modify want_new;
rename &renames1;
run;
quit;

%mend;

%rename(data1);


I suggest that you create a data set from your first sql so you can see the name(s) selected and what that string

"  name,'=&data1' " actually looks like.

 

You might consider looking into CALL EXECUTE with the data generated by the sql to have nicer tools to write lines of code instead of hoping that the macros you create, especially if there multiples that match your search requirement.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 2 replies
  • 765 views
  • 1 like
  • 2 in conversation