Autovariable renaming or copying
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | * (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. |
Related pages
...