Desktop productivity for business analysts and programmers

Automatically rename columns

Regular Learner
Posts: 1

Automatically rename columns



I have a table that I imported from excel into EG. This table is a description of my metadata.

It contains the tablenames/fieldnames but also their 'readable' descriptions of the tables/fields.


Is there an easy to query my data to this metadata-table and automatically convert my columnnames to the descriptions?


I'm finding it difficult to describe my problem so I do hope i'm making any sense.

I have attached some screenshots.

table description.jpgtable.jpg
Super User
Posts: 10,571

Re: Automatically rename columns

I would strongly advise against using the descriptions as column names, as they are not valid Base SAS names (alphabetic characters, numbers and underlines, no special characters and blanks allowed). While this is possible, it then constantly forces you to use the 'some interesting name'n construct when accessing columns.

Instead, you should set the column labels to the descriptions, as SAS output procedures can usually be directed to show the labels instead of the names.


So, once you have your description table in SAS, you could do the following:


data _null_;
set description_table end=done;
length execute_str $ 2000;
execute_str = catx(' ',trim(execute_str),trim(fieldname)!!'="'!!trim(description)!!'"');
if done then call symput('label_str',trim(execute_str));

Now you can use &label_str in a label statement.

Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 10,850

Re: Automatically rename columns

I see the variable name in metadata file is not exactly matched the name in sas table .

Is there any rule to map them . and Better post some sample data to enable us to test .

Super User
Super User
Posts: 9,840

Re: Automatically rename columns

One other option to the two answers provided above, is to have a normalised dataset, with the columns as parameters.  This is a common format in databases (and I am suprised to see a DB2 file with so many columns, doesn't seem like a good structure).  So if you have a file like this:




What your output dataset would look like is this:


VCDR_NR                ...

VEST_KD                 ...

AFBO_NR                ...



Then it should be a relatively simple task to merge your metadata on to get labels for each param:

PARAM_CODE         RESULT            PARAM

VCDR_NR                ...                     Aan Vestiging

VEST_KD                 ...                     ...

AFBO_NR                ...                     ...


You will find this structure can be easier to work with, for instance, if your doing calculations across columns, you have arrays, but you need to know and supply variable names etc.  If you were to stick with the transposed approach - wide - as you have now, I would recommend, not labelling column names as data, but use a generic term, say VAR1-VARx for instance.  Names of columns are to make programming easier, labels of columns are there to provide information.  Make your programming far simpler by following one of the above approaches.


Super User
Posts: 13,941

Re: Automatically rename columns

Somewhat of a kludge, but I have been known to use a spreadsheet with column information and use that with spreadsheet functions to create a column of LABEL statements to apply to variables. Using a full description in a label makes a lot of sense as you can have more characters and characters that are not acceptable in variable names.

Ask a Question
Discussion stats
  • 4 replies
  • 5 in conversation