Date: Thu, 19 Jan 2012 20:34:23 +0000
Reply-To: "Zdeb, Michael S" <mzdeb@ALBANY.EDU>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: "Zdeb, Michael S" <mzdeb@ALBANY.EDU>
Subject: Re: trimming last two characters from many variable names
In-Reply-To: <201201191803.q0JG7Zf7010743@waikiki.cc.uga.edu>
Content-Type: text/plain; charset="us-ascii"
hi ... one additional idea about using PROC SQL and PROC DATASETS, just in case there are any other embedded underscores in the variable names (e.g. Q404_890_1), it's probably a good idea to have the FIND command start searching from the RIGHT by specifying a START POSITION with a large negative value (see the -32 used below given that your searching variable names)
data x1;
retain xa_1 xb_1 x_c_1 1;
run;
data x2;
retain xa_2 xb_2 x_c_2 1;
run;
proc sql noprint;
select catt(name, '=' , substr(name,1, find(name,'_',-32)-1)) into :rename1 separated by ' '
from dictionary.columns where libname eq 'WORK' and memname eq 'X1';
select catt(name, '=' , substr(name,1, find(name,'_',-32)-1)) into :rename2 separated by ' '
from dictionary.columns where libname eq 'WORK' and memname eq 'X2';
quit;
proc datasets lib=work nolist;
modify x1;
rename &rename1 ;
modify x2;
rename &rename2 ;
quit;
Mike Zdeb
U@Albany School of Public Health
One University Place (Room 119)
Rensselaer, New York 12144-3456
P/518-402-6479 F/630-604-1475
________________________________________
From: SAS(r) Discussion [SAS-L@LISTSERV.UGA.EDU] on behalf of Alison [alisontetler@YAHOO.COM]
Sent: Thursday, January 19, 2012 1:03 PM
To: SAS-L@LISTSERV.UGA.EDU
Subject: trimming last two characters from many variable names
Hi all,
I am breaking up a horizontal dataset to restructure as vertical, and need
to rename many variables before setting the different datasets together.
Basically I have about 6 datasets now, with variables in first dataset:
Q404_1 Q405A_1 Q407AABB_1 for example (but with like 60 variables). Second
dataset variables are the same but with _2, and so forth and so on.
I am worried about making mistakes with using a rename array, and so I am
wondering how else I can program SAS to remove the right two characters from
these variable names.
Note that there are about 4 other variables in the dataset that do not have
the underscore number which should be excluded.
Thank you so much in advance,
Alison