Rename variables
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 | /*HELP! I have a list of variables that were created using the VECTOR command. I need to rename these variables so that the names are more meaningful. The dataset looks something like this (I'm in the middle of a transformation from cross- sectional to longitudinal data setup): ID relation height1 height2 ... height15 weight1 weight2 ... weight15 ... 1 mother 67 150 1 father 70 185 . 1 sibling13 62 135 I need to rename all the variables, so that the value of the relation variable is reflected in the appropriate name; in other words the varnames should look like this (excuse the long variable names, they are for illustration only; they will have 8 characters in the real dataset): ID heightmother heightfather ... heightsibling13 weightmother weightfather... Considering there are over 150 variables that I have to rename, I'd rather not have to rename everything by hand. Is there any way I can use a DO REPEAT syntax to create a loop that will rename everything for me? I would appreciate any help I can get on this one! Thanks, Sylvia */. ************************************. * This is Ray's answer posted on July 15,2000 to the usenet. *Define the variable names. NEW FILE. DATA LIST LIST /id(F8). BEGIN DATA END DATA. VECTOR height(15F8) weight(15F8) test(15F8). set mprint=on. DEFINE !rename (!POS=!CMDEND) !DO !var !IN (!1) !IF (!LENGTH(!var)<6) !THEN !LET !var2=!var !ELSE !LET !var2=!SUBSTR(!var,1,5) !IFEND RENAME VARIABLE (!CONCAT(!var,'1')=!CONCAT(!var2,'mdr')). RENAME VARIABLE (!CONCAT(!var,'2')=!CONCAT(!var2,'ftr')). !DO !cnt=1 !TO 13 RENAME VARIABLE (!CONCAT(!var,!LENGTH(!CONCAT(!BLANK(!cnt),'xx' )))=!CONCAT(!var2,'s',!cnt)). !DOEND !DOEND !ENDDEFINE. * Call the macro. !rename height weight test. |
Related pages
...