BookmarkSubscribeRSS Feed
Patrick
Opal | Level 21

@dirks

To be very clear: I'm only using such a conversion step when designing/implementing for "production worthy" ETL processes where I had to accept Excels as data sources; especially when using DIS.

 

My main reason for such an approach is to "keep the mess" in a separate job (the conversion to text files) and that once things fell over often enough in production because of an "instable" Excel data provision, "someone" will understand and make the effort to provide reasonable data sources - and then I can hopefully negotiate to get an interface contract which defines the text files I've already created - so one needs only to deactivate the conversion jobs.

 

Below the code sample you've asked for. If using DIS I'd implement this as a custom transformation passing in the parameter values via prompts.


/* macro to read Excel sheet and convert it to text file */
%macro xlsx2txt
  (
    infile,
    sheet_range,
    outfile,
    dlm
  );

  %if %str(&dlm)=%str() %then %let dlm=|;

  /* Proc Import step */
  proc import 
    datafile="&infile"
    dbms=xlsx
    out=work._temp_1234 
    replace;
    range="&sheet_range"
    ;
    getnames=NO;
  run;

  /* remove rows where all cells are empty */
  data work._temp_1234;
    set work._temp_1234;
    if cmiss(of _all_)>0 then delete;
  run;

  /* Proc Export step */
  proc export 
    data=work._temp_1234 
    outfile="&outfile"
    dbms=dlm 
    replace;
    delimiter="&dlm";
    putnames=no;
  run;

  /* delete temporary work table */
  proc datasets lib=work nolist nowarn;
    delete _temp_1234;
    run;
  quit;

%mend;



/* directory for sample data */
%let path=c:\test;

/* create sample Excel workbook and sheet */
libname test xlsx "&path\sample.xlsx";
data test.'sample sheet'n;
  set sashelp.class;
  format dttm_var datetime20.;
  dttm_var=datetime();
run;
libname test clear;

/* execute macro */
%xlsx2txt(&path\sample.xlsx,sample sheet$a2:f10000000,&path\sample.txt);

 

 

TomKari
Onyx | Level 15

I enjoy conversations about which tool sets are appropriate for which requirements. If you feel like discussing whether SAS is suitable for your needs in more detail, feel free to contact me offline

 

P.S. I don't work for SAS - I work in a marketing-free zone!

LinusH
Tourmaline | Level 20
@TomKari : you actually can, you just need to register WORK as a library in metadata. Whether this is a good idea in this case is a another question...
Data never sleeps
LinusH
Tourmaline | Level 20
@dirks : "what us the proper way to import Excel files"?
I think we answered that in your other post. 1st option is to have the producer of the data to send it in another format. If not feasible it's not a correct process to have SAS convert it. It adds unnecessary complexity to a simple process.
So the second best alternative is to use an Excel Library - hence no need for external file objects.
Data never sleeps
Patrick
Opal | Level 21

@LinusH

Using Proc Import with a Range allows to deal with these ugly Excels where you have multiple header rows with eventually merged cells.

LinusH
Tourmaline | Level 20

@Patrick: exception accepted Man Wink

Data never sleeps

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 20 replies
  • 1963 views
  • 4 likes
  • 7 in conversation