DATA Step, Macro, Functions and more

SOS

Reply
N/A
Posts: 0

SOS

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.
N/A
Posts: 0

Re: SOS

I am waiting for your reply on line. Thanks again.
Valued Guide
Posts: 632

Re: SOS

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?
Super User
Posts: 9,676

Re: SOS

[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
N/A
Posts: 0

Re: SOS

Dear Ksharp,

It's very kind of you to reply quickly and efficiently. Your program works very well. Many many thanks for your help.
Super User
Posts: 9,676

Re: SOS

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
Ask a Question
Discussion stats
  • 5 replies
  • 179 views
  • 0 likes
  • 3 in conversation