* (Q): I have variables whose names end with "_1", I need new variables whose names end by "_2". * (A): This is a generalisation by rlevesque@videotron.ca of the following solution posted by Rolf Kjoeller */. ********************************************************* ***** Original solution to the specific problem ***** ********************************************************* data list list /v_1 var_1 dhjl_1 dfgrjt_1 . begin data 1 2 3 4 8 7 6 5 end data . define !copy(!positional !cmdend) . !do !var !in (!1) . /* rename variables /* (!var=!concat(!substr(!var,1,!index(!var,"_1")),"2") ). compute !concat(!substr(!var,1,!index(!var,"_1")),"2") = !var . !doend . execute . !enddefine . !copy v_1 var_1 dhjl_1 dfgrjt_1 . ********************************************************* *** Generalisation by rlevesque@videotron.ca **** *** http://pages.infinit.net/rlevesqu/index.htm ********************************************************* DATA LIST LIST /v_1 var_1 dhjl_1 dfgrjt_1. BEGIN DATA 1 2 3 4 8 7 6 5 END DATA.. SET MPRINT=no /PRINTBACK=listing. *///////////////////////////////. DEFINE !varname (func=!TOKENS(1) /old=!ENCLOSE('(',')') /new=!ENCLOSE('(',')') /vlist=!CMDEND) !DO !var !IN (!vlist) /*calculate length of old string plus 1 */ !LET !lenold=!LENGTH(!CONCAT(!BLANK(!LENGTH(!old)),!BLANK(1))) /* n is the number of characters remaining unchanged */ !LET !n=!LENGTH(!SUBSTR(!BLANK(!LENGTH(!var)),!lenold)) /* Copy or rename variables */ !IF (!UPCASE(!func)=RENAME) !THEN RENAME VARIABLES (!var=!CONCAT(!SUBSTR(!var,1,!n),!new)). !ELSE COMPUTE !CONCAT(!SUBSTR(!var,1,!n),!new) = !var. !IFEND !DOEND EXECUTE. !ENDDEFINE. *///////////////////////////////. SET MPRINT=yes. * want to rename the original variables (replacing "_1" by "_2"). !varname func=rename old=(_1) new=(_2) vlist=v_1 var_1 dhjl_1 dfgrjt_1. LIST. * want to rename the original variables (replacing "_2" by "x"). !varname func=rename old=(_2) new=(x) vlist=v_2 var_2 dhjl_2 dfgrjt_2. LIST. * want to copy the original variables (replacing "x" by "yz"). !varname func=copy old=(x) new=(yz) vlist=vx varx dhjlx dfgrjtx. LIST.