DATA Step, Macro, Functions and more

How to rename a column in proc sql

Reply
Frequent Contributor
Posts: 124

How to rename a column in proc sql

Greetings all.  I'm having some difficulty renaming a column in proc sql.  Consider the following:

proc sql;

     create table sometable as

          select

               '0' as group_num,

               somereallylongcolumnname as short_name

            from sourcetable;

quit;

When I run this code, the first column is correctly named, as it should have been based on some documentation I found on the web.  However, the column 'somereallylongcolumnname' retains its name eventhough I used an alias.  I read where the alias will be displayed on reports and such, but I need the column to be renamed in the dataset.  I tried using (rename= at various places, but none seem to work.  I'm sure it is something simple, but I just can't get it.  Any ideas?  Thank you.

Greg

Frequent Contributor
Posts: 124

Re: How to rename a column in proc sql

This seemed to work

     somereallylongcolumnname as short_name labe='short_name'

Super User
Posts: 3,101

Re: How to rename a column in proc sql

Seems like you might be confusing column names with column labels. If you are sourcing your data from an external database often the label will be set to the same as the column name. The label is what is often used in reports. You could try the NOLABEL SAS option to avoid assigning labels when importing data.

Frequent Contributor
Posts: 124

Re: How to rename a column in proc sql

SASKiwi, thank you for the reply.  I'm not concerned about the label, its just that is the only way I could get the column to be named as I want.  My concern is that I need to use this dataset in some other stuff, and I would like to be able to reference the alias I named in the proc sql statement.  It works not, so I'm not sure if it's a label or the column name is correct, but I can reference short_name in the dataset.  Thanks.

Super Contributor
Posts: 418

Re: How to rename a column in proc sql

I agree with Kiwi. In your original code you actually DID rename the column, however you were still displaying its label of "somereallylongcolumnname", therefore when you open your dataset this is what you see (you acutally see the label and not the column name in the column header. You can see both if you go to the column properties)!

hope that helps

Super User
Posts: 17,748

Re: How to rename a column in proc sql

You would be able to reference it as short_name from your original step as well. It isn't an alias though, its a rename.

Ask a Question
Discussion stats
  • 5 replies
  • 567 views
  • 0 likes
  • 4 in conversation