10-01-2014 11:28 AM
How do I rename the Variables based on the following: I have variables like MAT___09_20091 MAT___09_20092 MAT___09_20093 Based on the last character 1 (abc) , 2 (xyz) and 3 (hgk), I want to rename them as abc_09_2009, xyz_09_2009, and hgk_09_2009. I may have various variables like this MAT___09_20101, MAT___09_20102 and so on. Also I want to rename the following field as MAT____09_2009, MAT____09_2010 as Growth_09_2009, Growth_09_2010 Thanks for any help..
10-01-2014 12:34 PM
I tried the following but it does not work. What I am doing wrong data want; set sashelp.vcolumn; where libname='WORK' and memname='TEST'; if substr(name,1,3) = "MAT" and substr(name,7,1) = "_" then call execute('proc datasets ;modify TEST;rename name = "Growth" || substr(name,7)'); run;
10-01-2014 12:45 PM
The approach looks quite feasible. One area where you are encountering trouble is the code being passed through CALL EXECUTE. In your DATA step, NAME refers to the name of a variable within sashelp.vcolumn. It should not be in quotes if you want it to be replaced with the NAME's value. So this string would be closer to what you need:
call execute('proc datasets; modify TEST; rename ' || name || '= Growth' || substr(name,7) || ';');
10-01-2014 01:10 PM
Great. A few tweaks then ...
Your data set WANT does not need to be saved. You could use data _null_ instead.
Your code is generating many instances of PROC DATASETS. You could generate just one instead:
if _n_=1 then call execute('proc datasets; modify TEST;');
Then the later IF/THEN statements could generate just the RENAME statements.
10-01-2014 01:16 PM
Astounding I got error for the following, how can I get rid of that error. Is there any string function I should be using. data _NULL_; set sashelp.vcolumn; where libname='WORK' and memname='TEST'; if substr(name,1,3) = "MAT" and substr(name,14,1) ^= " " then do; if substr(name,14,1) = "1" then do; call execute('proc datasets; modify TEST; rename ' || name || '= MA-ABC.GROWTH' || substr(name,6,13) || ';'); end; end; run; I got error for MA-ABC.GROWTH.
10-01-2014 02:41 PM
Well, names of variables in SAS are restricted to underscores, letters and numbers. Dashes and periods would not be valid characters for naming a variable.
There are ways around this, but they make future programming very cumbersome.