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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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