BookmarkSubscribeRSS Feed
Ivan555
Quartz | Level 8

Hello!

 

Could you please tell me how, when exporting data to Excel, to

> add some additional rows/columns in Excel spreadsheet being created

> add some text in specific cells

> add some text formats/background formats/border formats in specific cells

> customize some cells by combining them, also wrapping text, alignment text, rotating text, ...

> do auto alignment of cells and rows, also set the fixed size of rows and columns

 

I think this all can be solved if we are able to write VB code in SAS and transfer it to Excel.

But I can't find whether it is possible or not.

Could you, please, give me a link on this method(if it exists) or on other methods?

 

THX!

4 REPLIES 4
art297
Opal | Level 21

The exportxl macro (see: https://github.com/art297/Papers/blob/master/Excelling_to_Another_Level_with_SAS/src/exportxl.sas ) doesn't do all of the things you are trying to do, but does use the approach you were asking about. Hopefully, it will give you an idea regarding how to proceed.

 

The paper, powerpoint and tipsheet for the macro can be found at: https://github.com/art297/Papers/tree/master/Excelling_to_Another_Level_with_SAS/doc

 

Art, CEO, AnalystFinder.com

 

Reeza
Super User

> add some additional rows/columns in Excel spreadsheet being created

Not needed if you have Excel Tables that will dynamically change.

> add some text in specific cells

Named Ranges is one option, I prefer to use linked cells in my excel sheet instead. 

> add some text formats/background formats/border formats in specific cells

Yes, search traffic lighting

> customize some cells by combining them, also wrapping text, alignment text, rotating text, ...

Some of these yes, not combining cells I believe

> do auto alignment of cells and rows, also set the fixed size of rows and columns

Yes

 


Some of what you want is available in ODS EXCEL but not all.

 

DDE allowed you to do most of this but is deprecated. You can use DDE to pass VBA command to Excel though or through a VBS script. If I'm developing these types of reports, I usually make a template and link the cells in Excel rather than worrying about writing to exact cells. I've done all of these, but definitely try and avoid it as much as possible these days. 

 

If you search DDE on lexjansen.com you'll find code examples for most of these actions. The actions need to have been available in Excel 4.0 though if you go DDE. If you go VBS you have more options. 

s_lassen
Meteorite | Level 14

One technique I have used often, not just in SAS, but also in SSIS, is to have a "template" - not a template as Excel understands the word, but an empty workbook that is formatted the way I want it.

 

You can then have a sheet containing your data, formatted the way you want, and perhaps another sheet or two which present the data in a pivot table or whatever.

 

With that, you first copy your template workbook to wherever you want the output (using FCOPY or X command), then you assign the new workbook as a SAS libname, and write (SAS datastep, SQL select or whatever) to the sheet where the data is supposed to be.

 

Hope this is helpful.

art297
Opal | Level 21

@s_lassen : While I mentioned the exportxl macro simply in the hope that it could provide @Ivan555 with an idea on how to incorporate vb script into SAS code, it includes a way to create a table based on either an existing table or an explicit Excel template.

 

Art, CEO, AnalystFinder.com

 

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
  • 596 views
  • 2 likes
  • 4 in conversation