Hi Everyone,
I have 2 dataset that have similar variables. One file is for daily and the other file is for monthly.
I want to add a term say "_daily" after all variables in daily file and "_monthly" for those in monthly file.
Is there any way to do so instead of using the rename one by one?
Thank you.
HC
data have;
input time var1x var3;
datalines;
1 1 1
2 3 5
1 2 3
;
run;
data have;
input time var1x var3;
datalines;
1 1 1
2 3 5
1 2 3
;
run;
data _null_;
set sashelp.vcolumn(where=(libname='WORK' and memname='HAVE') keep=libname memname name) end=last;
if _n_ eq 1 then call execute('proc datasets library=work nolist nodetails;modify have;rename');
call execute(cats(name,'=',name,'_daily'));
if last then call execute(';quit;');
run;
Use metadata (SASHELP.VCOLUMN or DICTIONARY.COLUMNS or output of PROC CONTENTS) to generate the rename code.
proc sql noprint ;
select catx('=',name,cats(name,'_monthly'))
into :monthly separated by ' '
from dictionary.columns
where libname='WORK'
and memname='MONTHLY'
;
quit;
data want ;
set monthly (rename=(&monthly)) ;
run;
Slight modification to Tom's code - I would suggest using proc datasets to modify the variable names instead.
If you're merging the files though, do it in that step instead of a separate rename step.
data have;
input time var1x var3;
datalines;
1 1 1
2 3 5
1 2 3
;
run;
data _null_;
set sashelp.vcolumn(where=(libname='WORK' and memname='HAVE') keep=libname memname name) end=last;
if _n_ eq 1 then call execute('proc datasets library=work nolist nodetails;modify have;rename');
call execute(cats(name,'=',name,'_daily'));
if last then call execute(';quit;');
run;
Hi,
I used this code and it worked successfully. But before this step I want to remove "_" from all my variable names - is there a quick way? I tried using compress with your code but failed.
Thank you.
The same steps should apply. Try just running the part that calculates the new variable name from the old name in a data step so you can look at the result and figure what is happening.
new_name = compress(name,'_');
if not findc(name,'_') then call execute(cats(name,'=',name,'_daily'));
Thank you can I apply the same logic or code to change the labels for all my variables - i mean i want to add a term before the label name for all vars:
something like this:
PROC CONTENTS DATA=sat1 OUT=tmp_sat2;
RUN;
data _null_;
set WORK.tmp_SAT2(where=(libname='WORK' and memname='SAT1') keep=libname memname label) end=last;
if _n_ eq 1 then call execute('proc datasets library=work nolist nodetails;modify SAT1;LABEL');
call execute(cats(label,'=','SAT_',label));
if last then call execute(';quit;');
run;
Your feedback is highly appreciated
Hi,
Its giving me errors - Variable not found!
Help
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.