BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
HitmonTran
Pyrite | Level 9

Hello, Not sure if this will be a simple process, but is it fairly simple to import an excel file (eg ADSL specs) and convert them into dataset variable names, length, and label?

 

HitmonTran_0-1695975942435.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Is the question how to convert an Excel sheet into a SAS dataset?

Or is the question once you have dataset with variables like VARNUM, NAME, TYPE, LENGTH, FORMAT and LABEL how to use that to define a SAS dataset?

First convert data to ATTRIB statements.

filename code temp;
data _null_;
  set specs ;
  by varnum;
  length vname vlength vformat $60 ;
  vname=nliteral(name);
  if lowcase(type)='char' then vlength=cats('length=$',length);
  else vlength=cats('length=',max(8,length));
  if format ne ' ' then vformat=cats('format=',format);
  put 'attrib ' vname vlength vformat label=:$quote. ';' ;
run;

Then you use %INCLUDE to use those ATTRIB statements to define the dataset.

 

For example you could use a step like this to make an empty dataset with those variables.

data want;
  %include code / source2;
  call missing(of _all_);
  stop;
run;

 

View solution in original post

4 REPLIES 4
Amir
PROC Star

Hi,

 

SAS can read Excel files, an example taken from SAS blog Using LIBNAME XLSX to read and write Excel files  is shown below:

 

/* because Excel field names often have spaces */
options validvarname=any;
 
libname xl XLSX '/folders/myfolders/sas_tech_talks_15.xlsx';
 
/* read in one of the tables */
data confirmed;
  set xl.confirmed;
run;
 
libname xl CLEAR;

 

 

Thanks & kind regards,

Amir.

Tom
Super User Tom
Super User

Is the question how to convert an Excel sheet into a SAS dataset?

Or is the question once you have dataset with variables like VARNUM, NAME, TYPE, LENGTH, FORMAT and LABEL how to use that to define a SAS dataset?

First convert data to ATTRIB statements.

filename code temp;
data _null_;
  set specs ;
  by varnum;
  length vname vlength vformat $60 ;
  vname=nliteral(name);
  if lowcase(type)='char' then vlength=cats('length=$',length);
  else vlength=cats('length=',max(8,length));
  if format ne ' ' then vformat=cats('format=',format);
  put 'attrib ' vname vlength vformat label=:$quote. ';' ;
run;

Then you use %INCLUDE to use those ATTRIB statements to define the dataset.

 

For example you could use a step like this to make an empty dataset with those variables.

data want;
  %include code / source2;
  call missing(of _all_);
  stop;
run;

 

ballardw
Super User

For  your "derived" variables you may want to have someone investigate CATX instead of strip()|| strip.

I have a minor concern that your derived variables are attempting to use variables that may not be defined in the order they occur. So this is likely going to take two or three passes through the junk to create appropriate attributes and the additional code that seems to be implied.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 915 views
  • 0 likes
  • 5 in conversation