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-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
  • 4 replies
  • 531 views
  • 0 likes
  • 5 in conversation