1
votes

I would like to delete the last 3 letters from a set of variables in a SAS dataset. (have to concatenante 2 datasets. )

And in the first set, the variables are named for example: abc , def , ghi ...

While in the 2nd set they are named: abc_1A , def_1A , ghi_1A ...

How can I delete the '_1A' from the 100+ variables? I don't want to simply add '_1A' to my first Dataset

Thanks

1

1 Answers

2
votes

There are a few options. Here is one 'safe' option and one 'unsafe' option.

Safe option:

Write a macro to rename a variable to its name without the _1A.; Arguments: var = variable name, len = length desired of final variable name.;

%macro rename_shorter(var=,len=);
&var. = %substr(&var.,1,&len.)
%mend rename_shorter;

Create a list of calls from dictionary.columns;

proc sql;
select cats('%rename_shorter(var=',name,',len=',length(name)-3,')') 
 into :renamelist separated by ' '
 from dictionary.columns
 where libname='SASHELP' and upcase(memname)='CLASS';
quit;

Call that list;

data want;
set sashelp.class(rename=(&renamelist.));
run;

An unsafe option, in the sense that it doesn't check that things are aligned correctly, is

proc sql;
create table want as 
 select * from have_namedright H
 outer union corr
 select * from have_namedwrong W
;
quit;