BookmarkSubscribeRSS Feed
dxhuan
Calcite | Level 5

Hi SAS Users,

I have a bunch of xls files (too many for manual adjustment). I want to read them into SAS with the types of all columns being character. And I also would like to force all the columns to have the same length, say $5000. Within each xls, I have 60 columns, so i'd prefer programming to manual change.

My SAS programming skill is intermediate I would say. And according to my knowledge, neither PROC IMPORT nor the LIBNAME excel engine can easily achieve this. Is DATA step my best option? Anybody any help? Thanks.

6 REPLIES 6
Ksharp
Super User

You'd better transform all these XLS files into CSV files . Search a tool to do that in Google .

And after that :

infile dummy filevar=fname .........

..........

input ( var1-var60) (: $200.);

dxhuan
Calcite | Level 5

Hi Xia,

Thanks. I tried to transform the XLS to CSV. But there's one column containing ";" separating names. When I save the XLS as CSV, all information after the semicolons are lost. I need to keep these semicolons for future use, so i can't dump these semicolons either. I've also tried to transfer XLS to TXT. Failed too.

Ksharp
Super User

That is really weird . then proc import it and transform all numeric into character .

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Have just done a quick check and Excel seems to handle semicolons fine.  Post your WB or sample as it should work fine.

data_null__
Jade | Level 19

You could use the LIBNAME engine and DBSASTYPE to achieve what you want.  It takes a bit of doing (code gen);

options validvarname=any validmemname=extend center=0;
libname x1 excel "system-specific pathname.xlsx";
proc contents noprint data=X1._all_ order=varnum out=names(keep=libname memname name varnum);
   run;
proc sort data=names;
   by libname memname varnum;
   run;
proc print;
  
run;
filename FT76F001 temp;
data _null_;
  
file FT76F001;
   length memname name $35;
  
set names;
   by libname memname;
   if first.memname then do;
      memname = nliteral(memname);
     
put 'data ' memname ';';
      put +
3 'set ' libname '.' memname '(DBSASTYPE=(';
     
end;
  
   name = nliteral(name);
   put +
12 name "='char(200)'";
  
if last.memname then do;
      put +
12 '));';
      put +
3 'run;';
     
end;
  
run;
%inc FT76F001 / source2;
libname x1 clear;
proc contents data=work._all_ order=varnum;
  
run;


/*DBSASTYPE=(<column-name-1> =<SAS data-type> ...<column-name-n> = <SAS data-type> )*/
/*SAS data-type*/
/*specifies a SAS data type. SAS data types include CHAR(n), NUMERIC, DATETIME, DATE, TIME.*/


3-18-2015 8-48-16 AM.png
dxhuan
Calcite | Level 5

Thank you very much, data_null_. It works well.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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