BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Dear all,

I have a data set like this.
group x1_N x1_nmiss x1_mean x1_stddev x1_median
1 201 0 15.3 5.2 14.9
2 199 0 17.6 4.9 17.5
3 203 0 18.9 5.7 18.6

How can I use the macro to remove the character "x1_" of every variable(to rename x1_nmiss as nmiss,etc.)? I can finish the work with data step very well, but when I try to use macro, the problem occures. Who can give me a hand? Thanks in advance for your time and patience.
5 REPLIES 5
deleted_user
Not applicable
I am waiting for your reply on line. Thanks again.
ArtC
Rhodochrosite | Level 12
Should we assume that the actual problem has a large enough number of variables to preclude just coding a rename statement? Are you trying to build a macro that will rename all variables in a data set that start with some given string?
Ksharp
Super User
[pre]
data temp;
infile datalines dlm=' ';
input group x1_N x1_nmiss x1_mean x1_stddev x1_median;
datalines;
1 201 0 15.3 5.2 14.9
2 199 0 17.6 4.9 17.5
3 203 0 18.9 5.7 18.6
;
run;

options mprint mlogic symbolgen;
%macro rename;
proc sql ;
select name from dictionary.columns where libname='WORK' and memname='TEMP' and name like 'x1%';
select name
into : name1 - : name&sqlobs.
from dictionary.columns where libname='WORK' and memname='TEMP' and name like 'x1%';
quit;


proc datasets library=work memtype=data nolist;
modify temp;
rename %do k=1 %to &sqlobs.;
&&name&k.. = %sysfunc(scan( &&name&k.. ,2,_) )
%end;
;
quit;
%mend;

%rename
[/pre]



Ksharp
deleted_user
Not applicable
Dear Ksharp,

It's very kind of you to reply quickly and efficiently. Your program works very well. Many many thanks for your help.
Ksharp
Super User
Maybe this code is better.


[pre]
data temp;
infile datalines dlm=' ';
input group x1_N x1_nmiss x1_mean x1_stddev x1_median;
datalines;
1 201 0 15.3 5.2 14.9
2 199 0 17.6 4.9 17.5
3 203 0 18.9 5.7 18.6
;
run;

options mprint mlogic symbolgen;
%macro rename;
proc sql ;
select name from dictionary.columns where libname='WORK' and memname='TEMP' and name like 'x1%';
select catx(' ',name,'=',scan(name,2,'_'))
into : name1 - : name&sqlobs.
from dictionary.columns where libname='WORK' and memname='TEMP' and name like 'x1%';
quit;


proc datasets library=work memtype=data nolist;
modify temp;
rename %do k=1 %to &sqlobs.;
&&name&k..
%end;
;
quit;
%mend;

%rename
[/pre]


Ksharp

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
  • 5 replies
  • 1299 views
  • 0 likes
  • 3 in conversation