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