BookmarkSubscribeRSS Feed
GreyBlack
Calcite | Level 5
Good evening, everyone!

So I have an xlsx file that I'm trying to import into Excel that doesn't have any column names within the actual file. I'm able to use the proc import to get the data out from the file, but it reads row 1 as the names of the columns.

Does anyone have a way to name the columns of an xlsx data set as you're importing?

For reference, my code right now is:

Proc import out=data1 file='c:\filename.xlsx' dbms=xlsx;
Sheet="sheet1";
Getnames=no;
Run;
5 REPLIES 5
Tom
Super User Tom
Super User

I do not understand what you are asking since the code you showed does import an XLSX sheet that does not have a header row.

 

Example:

filename xlsx temp;
proc export dbms=xlsx file=xlsx data=sashelp.class;
  putnames=no;
run;

proc import dbms=xlsx file=xlsx out=want replace;
  getnames=no;
run;

Tom_0-1670198647244.png

 

If you know the names you want to use include them in the output dataset name using the RENAME= dataset option.

proc import dbms=xlsx file=xlsx out=class(rename=(a=name b=sex c=age d=height e=weight)) replace;
  getnames=no;
run;
GreyBlack
Calcite | Level 5
Perhaps I should rephrase; I'm hoping to add headers after performing the
import, but I'm not clear on how to do that.
Tom
Super User Tom
Super User

@GreyBlack wrote:
Perhaps I should rephrase; I'm hoping to add headers after performing the
import, but I'm not clear on how to do that.

That makes even less sense.  Once the file is imported it is a dataset.  Datasets do not have headers. The variables in a dataset have names.  If you know the names you want to use for the variables you can include that in the code using the RENAME= dataset option.

proc import dbms=xlsx file=xlsx out=class(rename=(a=name b=sex c=age d=height e=weight)) replace;
  getnames=no;
run;
Kurt_Bremser
Super User

The usual remedy: save to a .csv file and read that with a DATA step, where you set all names, types and attributes without relying on the guesses of PROC IMPORT.

Patrick
Opal | Level 21

@GreyBlack wrote:
Perhaps I should rephrase; I'm hoping to add headers after performing the
import, but I'm not clear on how to do that.

Your variable names will be A, B, C,...

If you need to rename them then use either a rename statement within a SAS datastep or if you don't need to process the data then use Proc Datasets. 

Below some sample code using Proc Datasets.

data work.imported_from_excel;
  a=1;
  b='a';
  c=99;
run;

proc datasets lib=work nolist;
  modify imported_from_excel;
    rename 
      a=myvar
      b=some_other_name
      c=new_name
      ;
  run;
quit;

Patrick_0-1670228636499.png

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 5 replies
  • 1532 views
  • 1 like
  • 4 in conversation