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..
any help???
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;
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) || ';');
Good luck.
Thanks Astounding it worked..
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.
Good luck.
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.
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.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.