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
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.
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.
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);
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.
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!
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.