DATA Step, Macro, Functions and more

renaming a variable inside a macro

Accepted Solution Solved
Reply
Super Contributor
Posts: 455
Accepted Solution

renaming a variable inside a macro

Hi,

 

I have the following macro:

 

%macro varz(variable);
data qc2;
set qc;
keep gvkey fyear &variable;
rename &variable=&variable.._qc;
run;
%mend;

%varz(sales);

So here I want to create a data table qc2 which will include the variables gvkey, fyear and sales from the original table qc, and then rename sales to sales_qc, but it is this renaming part which causes errors (if it is omitted then the macro works).

 

Thank you


Accepted Solutions
Solution
‎10-02-2017 10:22 PM
Regular Contributor
Posts: 195

Re: renaming a variable inside a macro

Don't use a datastep to rename a variable, use proc datasets instead, there is no need to do a full iteration, when you just want to change metadata. And: why do you want a macro? 

 

To solve the error in your code, reduce the number of dots between &variable and _qc to one.

 

 

View solution in original post


All Replies
Solution
‎10-02-2017 10:22 PM
Regular Contributor
Posts: 195

Re: renaming a variable inside a macro

Don't use a datastep to rename a variable, use proc datasets instead, there is no need to do a full iteration, when you just want to change metadata. And: why do you want a macro? 

 

To solve the error in your code, reduce the number of dots between &variable and _qc to one.

 

 

Super Contributor
Posts: 455

Re: renaming a variable inside a macro

Posted in reply to error_prone

I have 2 datasets: qc and cc, and there are variables with identical names in both datasets. I just wanted a quick way to compare the variables from the datasets. Here is the full code:

 

%macro varz(variable);
data qc2;
set qc;
keep gvkey fyear &variable;
rename &variable=&variable._qc;
run;

data cc2;
set cc;
keep gvkey fyear &variable;
rename &variable=&variable._cc;
run;

data want;
merge qc2 cc2;
by gvkey fyear;
run;

data want;
set want;
diff = &variable._qc - &variable._cc;
run;

proc means data=want;
run;

%mend;

%varz(sale);
Respected Advisor
Posts: 4,548

Re: renaming a variable inside a macro

@ilikesas

The following might give you the idea how you could go about this.

data work.demo;
  set sashelp.class;
run;

proc sql noprint;
  select
    cats(name,'= demo_',name) 
      into :rename_list separated by ' '
  from dictionary.columns
  where libname='WORK' and memname='DEMO' and upcase(name) not in ('NAME','HEIGHT')
  ;
quit;

proc datasets lib=work nolist;
  modify demo;
    rename &rename_list;
  run;
quit;

proc contents data=demo;
run;quit;

Alternatively consider using Proc Compare.

Regular Contributor
Posts: 195

Re: renaming a variable inside a macro

Automatically renaming variables will work only if you are 100% sure, that each variables name with the appended postfix is shorter than 32 chars.

Depending on what you want to do with the result of the comparison, using proc compare can be the easier solution.
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 528 views
  • 3 likes
  • 3 in conversation