BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
matoma
Obsidian | Level 7

I'm trying to import a few excel files and when I do so I want to change the column/variable names but I'm not sure how to do that. Right now they're too long or just showing up as b,c,d,etc. Heres my import code.

proc import out=facts5

datafile="C:\Users\Downloads\SAS2\2019_FACTS_Table_A-2.5.xlsx" DBMS=xlsx replace;

getnames=YES;

datarow=6;

run;

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

You have a couple of choices.

If you have already imported the data you can change properties of variables like name and label with Proc Datasets and a modify statement.

Or you can modify the spreadsheet before importing it.

Or save the spreadsheet to a text format such as CSV and write a data step to name the variables as needed. Proc Import could be used to build a template of the code needed to read the csv by importing, copying the code generated from the log to the editor and then editing the informat, format and input statements to a nicer name using search and replace (carefully).

 

The following code would rename variables in Facts5 from A, B , C to Variable1, Variable2 and Variable3

proc datasets library=work;
   modify  facts5  ;
   rename a = variable1
          b = variable2
          c = variable3
   ;
run;
quit;

If you have multiple datasets than need the same change you use multiple MODIFY statements to indicate each data set but could copy the rename statement for each set.

 

View solution in original post

1 REPLY 1
ballardw
Super User

You have a couple of choices.

If you have already imported the data you can change properties of variables like name and label with Proc Datasets and a modify statement.

Or you can modify the spreadsheet before importing it.

Or save the spreadsheet to a text format such as CSV and write a data step to name the variables as needed. Proc Import could be used to build a template of the code needed to read the csv by importing, copying the code generated from the log to the editor and then editing the informat, format and input statements to a nicer name using search and replace (carefully).

 

The following code would rename variables in Facts5 from A, B , C to Variable1, Variable2 and Variable3

proc datasets library=work;
   modify  facts5  ;
   rename a = variable1
          b = variable2
          c = variable3
   ;
run;
quit;

If you have multiple datasets than need the same change you use multiple MODIFY statements to indicate each data set but could copy the rename statement for each set.

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 384 views
  • 0 likes
  • 2 in conversation