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;
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.
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.