Skip to content

Instantly share code, notes, and snippets.

@Cesar-Urteaga
Created March 4, 2017 23:04
Show Gist options
  • Save Cesar-Urteaga/698580122ed53edd0b425fcd968d0125 to your computer and use it in GitHub Desktop.
Save Cesar-Urteaga/698580122ed53edd0b425fcd968d0125 to your computer and use it in GitHub Desktop.
Renames the fields of a table based on a string with field names.
/*-----------------------------------------------------------------------------|
| Description : Renames the fields of a table based on a string with field |
| names. |
| Assumptions : Parameters' table names have the library attached. |
| Parameters : InputTable - Table with the original names. |
| OutputTable - Table with the new names (Default: InputTable). |
| OldNames - String with the old names separated by spaces. |
| NewNames - String with the new names separated by spaces. |
| PrefixCharacter - A prefix to be used to define the new names. |
| Output : A new table (or an overwritten one) with the new names. |
|-----------------------------------------------------------------------------*/
/* Examples:
* Creates a sample SAS data set so as to execute the examples. ;
DATA WORK.TABLE;
A = 1; B = .; C = "Foo";
RUN;
* Ex_01;
%MRenameFields(InputTable = WORK.TABLE,
OutputTable = WORK.COPY_TABLE,
OldNames = A B C,
NewNames = X Y Z);
* Output: Create a copy of the table 'WORK.COPY_TABLE' with the fields A, B,
and C renamed as X, Y, and C. ;
* Ex_02;
%MRenameFields(InputTable = WORK.TABLE,
OldNames = A B C,
NewNames = M N O);
* Output: Renames the fields A, B, and C with the new names M, N, and O. ;
* Ex_03;
%MRenameFields(InputTable = WORK.TABLE,
OldNames = M N O,
PrefixCharacter = _);
* Output: Renames the fields M, N, and O with the new names _M, _N, and _O. ;
*/
/*-----------------------------------------------------------------------------|
| Date Author Description |
|------------------------------------------------------------------------------|
| July 15, 2016 Cesar R. Urteaga-Reyesvera Creation. |
|-----------------------------------------------------------------------------*/
%MACRO MRenameFields(InputTable = /* Table with the old names; it must
include the library. */,
OutputTable = &InputTable. /* Table with the new names;
it must include the
library. */,
OldNames = /* String with the old names separated by
spaces. */,
NewNames = /* String with the new names separated by
spaces. */,
PrefixCharacter = /* A prefix character to be used to
define the new names. */
);
%LOCAL li
lOldName
lNewName;
* We create a table only if there is a specified output table. ;
%IF &OutputTable ~= &InputTable %THEN
%DO;
DATA &OutputTable.;
SET &InputTable.;
RUN;
%END;
* We substitute the old names with the new ones. ;
PROC DATASETS LIBRARY = %SCAN(&OutputTable, 1, .);
MODIFY %SCAN(&OutputTable, 2, .);
RENAME
%LET li = 1;
%LET lOldName = %SCAN(&OldNames, &li);
%IF &PrefixCharacter = %THEN
%LET lNewName = %SCAN(&NewNames, &li);
%ELSE
%LET lNewName = &PrefixCharacter.%SCAN(&OldNames, &li);
%DO %WHILE(&lOldName ~= );
&lOldName = &lNewName
%LET li = %EVAL(&li + 1);
%LET lOldName = %SCAN(&OldNames, &li);
%IF &PrefixCharacter = %THEN
%LET lNewName = %SCAN(&NewNames, &li);
%ELSE
%LET lNewName = &PrefixCharacter.%SCAN(&OldNames, &li);
%END;
;
RUN;
%MEND MRenameFields;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment