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: Call for Content

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!

Submit your idea!

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